YPS-Beer/backend/YPS.Beer/Services/BeerService.cs
2023-12-07 00:20:59 +00:00

37 lines
877 B
C#

using Microsoft.EntityFrameworkCore;
using YPS.Beer.Data;
using YPS.Beer.Models;
namespace YPS.Beer.Services;
public class BeerService : IBeerService
{
private readonly BeerContext _context;
public BeerService(BeerContext context)
{
_context = context;
}
public async Task<User?> GetUserById(string userId) =>
await _context.Users
.Include(u => u.Favourites)
.SingleOrDefaultAsync(u => u.Id == userId);
public async Task<Favourite?> AddFavouriteToUser(string userId, Favourite favourite)
{
var user = await GetUserById(userId);
if (user is null)
return null;
if (user.Favourites.Any(f => f.BeerId == favourite.BeerId))
return null;
user.Favourites.Add(favourite);
await _context.SaveChangesAsync();
return favourite;
}
}