public async Task <FavouritePokemon> AddFavourite(int id, FavouritePokemonModel model) { var entity = model.ToEntity(); entity.UserId = id; var user = await _context.Users.FindAsync(id); if (user.FavouritePokemons.Any(x => x.PokemonId == model.FavouritePokemonId)) { throw new PokemonAPIException("This pokemon already exists!", ExceptionConstants.BAD_REQUEST); } await _context.FavouritePokemons.AddAsync(entity); try { await _context.SaveChangesAsync(); } catch (Exception) { throw; } return(entity); }
public static FavouritePokemon ToEntity(this FavouritePokemonModel model) { return(new FavouritePokemon { Id = model.Id, PokemonId = model.FavouritePokemonId }); }
public async Task <FavouritePokemonModel> AddFavourite([FromBody] FavouritePokemonModel model) { var x = await _service.AddFavourite(Convert.ToInt32(this.User.Identity.Name), model); return(x.ToModel()); }