Пример #1
0
 public static RecipeRating FromApiToRecipeRating(ApiRecipeRating rec)
 {
     return(new()
     {
         Rating = rec.Rating,
         UserId = rec.UserId,
         RecipeId = rec.RecipeId
     });
 }
Пример #2
0
        public async Task <IActionResult> DeleteRecipeRating(ApiRecipeRating apiObj)
        {
            var ratingToDelete = RecipeRating.FromApiToRecipeRating(apiObj);

            _context.Entry(ratingToDelete).State = EntityState.Deleted;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeRatingExists(ratingToDelete))
                {
                    return(NotFound());
                }

                throw;
            }

            return(NoContent());
        }
Пример #3
0
        public async Task <ActionResult <ApiRecipeRating> > PostRecipeRating(ApiRecipeRating apiRecipeRating)
        {
            //convert api model to regular model
            var recipeRating = RecipeRating.FromApiToRecipeRating(apiRecipeRating);
            await _context.RecipeRating.AddAsync(recipeRating);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (RecipeRatingExists(recipeRating))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetRecipeRating", new { id = apiRecipeRating.UserId }, apiRecipeRating));
        }
Пример #4
0
        public async Task <IActionResult> PutRecipeRating(string userId, int RecipeId, ApiRecipeRating apiObj)
        {
            //convert input to database object
            var updatedRating = new RecipeRating()
            {
                UserId   = userId,
                RecipeId = RecipeId,
                Rating   = apiObj.Rating
            };

            //notifier to entity framework core
            _context.Entry(updatedRating).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeRatingExists(updatedRating))
                {
                    return(NotFound());
                }

                throw;
            }

            return(NoContent());
        }