36 lines
998 B
C#
36 lines
998 B
C#
using API.Models.RecipeModels;
|
|
using API.Models.ShoppingListModels;
|
|
using API.Models.UserModels;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace API.DBAccess
|
|
{
|
|
public class ShoppingListDBAccess
|
|
{
|
|
private readonly DBContext _context;
|
|
|
|
public ShoppingListDBAccess(DBContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
public async Task<User> ReadShoppingList(int userId)
|
|
{
|
|
var user = await _context.Users.Include(u => u.ShoppingList).FirstOrDefaultAsync(u => u.Id == userId);
|
|
|
|
return user;
|
|
}
|
|
|
|
public async Task<IActionResult> UpdateShoppingList(User user)
|
|
{
|
|
_context.Entry(user).State = EntityState.Modified;
|
|
|
|
bool saved = await _context.SaveChangesAsync() >= 1;
|
|
|
|
if (saved) { return new OkObjectResult(true); }
|
|
|
|
return new ConflictObjectResult(new { message = "Could not save to database" });
|
|
}
|
|
}
|
|
}
|