Пример #1
0
        public async Task <IActionResult> Patch(int id, [FromBody] NovelPatchDto value)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (id < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(id), "Id should be greater than 0.");
                }

                var updatedNovel = await _novelsService.UpdateNovel(id, value);

                return(AcceptedAtRoute("GetNovel", new { id = updatedNovel.Id }, updatedNovel));
            }
            catch (DeepValidationException e)
            {
                _logger.LogMethodError(e);

                ModelState.AddModelError(e.Key, e.Error);

                return(BadRequest(ModelState));
            }
            catch (Exception e)
            {
                _logger.LogMethodError(e);

                return(BadRequest(e.Message));
            }
        }
Пример #2
0
        public async Task <NovelDto> UpdateNovel(int id, NovelPatchDto updates)
        {
            var novel = await GetNovelsWithDeepData().FirstOrDefaultAsync(x => x.Id == id);

            if (novel == null)
            {
                throw new ResourceNotFoundException();
            }

            if (updates.Title != null)
            {
                novel.Title = updates.Title;
            }

            if (updates.AltTitles != null)
            {
                novel.AltTitlesCollection = updates.AltTitles.ToImmutableArray();
            }

            if (updates.TypeId != null)
            {
                var type = await _dbContext.Types.FindAsync(updates.TypeId);

                novel.Type = type
                             ?? throw new DeepValidationException(nameof(updates.TypeId),
                                                                  $"Type with id:{updates.TypeId} was not found");
            }

            if (updates.GenreIds != null)
            {
                novel.Genres = new List <NovelGenre>();
                foreach (var genreId in updates.GenreIds)
                {
                    var genre = await _dbContext.Genres.FindAsync(genreId);

                    if (genre == null)
                    {
                        throw new DeepValidationException(nameof(updates.GenreIds),
                                                          $"Genre with id:{genreId} was not found");
                    }

                    novel.Genres.Add(new NovelGenre {
                        Genre = genre
                    });
                }
            }

            if (updates.TagIds != null)
            {
                novel.Tags = new List <NovelTag>();
                foreach (var tagId in updates.TagIds)
                {
                    var tag = await _dbContext.Tags.FindAsync(tagId);

                    if (tag == null)
                    {
                        throw new DeepValidationException(nameof(updates.TagIds), $"Tag with id:{tagId} was not found");
                    }

                    novel.Tags.Add(new NovelTag {
                        Tag = tag
                    });
                }
            }

            await _dbContext.SaveChangesAsync();

            return(_mapper.Map <NovelDto>(novel));
        }