예제 #1
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Author author;

            try
            {
                author = await authorService.GetAuthorAsync(id.Value);
            }
            catch (ApplicationException)
            {
                return(NotFound());
            }

            AuthorDto = new UpdateAuthorDto
            {
                Id                 = author.Id,
                FirstName          = author.FirstName,
                MiddleName         = author.MiddleName,
                LastName           = author.LastName,
                Suffix             = author.Suffix,
                ContactEmail       = author.ContactEmail,
                ContactPhoneNumber = author.ContactPhoneNumber,
                Description        = author.Description,
                WebsiteURL         = author.WebsiteURL
            };

            return(Page());
        }
 public IActionResult Put(int id, [FromBody] UpdateAuthorDto dto,
                          [FromServices] IUpdateAuthorCommand command)
 {
     dto.Id = id;
     _executor.ExecuteCommand(command, dto);
     return(NoContent());
 }
예제 #3
0
        public async Task <Author> UpdateAuthorAsync(UpdateAuthorDto authorDto)
        {
            var author = await this.db.SelectAuthorByIdAsync(authorDto.Id);

            author.FirstName          = authorDto.FirstName;
            author.MiddleName         = authorDto.MiddleName;
            author.LastName           = authorDto.LastName;
            author.Suffix             = authorDto.Suffix;
            author.ContactEmail       = authorDto.ContactEmail;
            author.ContactPhoneNumber = authorDto.ContactPhoneNumber;
            author.Description        = authorDto.Description;
            author.WebsiteURL         = authorDto.WebsiteURL;

            try
            {
                this.ValidateAuthorOnUpdate(author);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Attempted to add invalid author.");
                throw;
            }

            return(await db.UpdateAuthorAsync(author));
        }
예제 #4
0
        public async Task UpdateAuthorAsync(UpdateAuthorDto updateAuthor)
        {
            var existingPost = await _authorRepository.GetAuthorByIdAsync(updateAuthor.Id);

            var post = _mapper.Map(updateAuthor, existingPost);
            await _authorRepository.UpdateAsync(post);
        }
예제 #5
0
        public async Task <ServiceResult> Update(UpdateAuthorDto author)
        {
            try
            {
                var storedAuthor = await _unitOfWork.Repository <Author>().GetById(author.Id);

                if (storedAuthor == null)
                {
                    return(Error("There is not such author"));
                }

                var newAuthor = new Author
                {
                    Id   = storedAuthor.Id,
                    Name = author.Name ?? storedAuthor.Name,
                    Year = (author.Year != 0) ? author.Year : storedAuthor.Year
                };

                _unitOfWork.Repository <Author>().Update(newAuthor);
                await _unitOfWork.SaveAsync();

                return(Success());
            }
            catch
            {
                return(Error("Can't update author"));
            }
        }
예제 #6
0
        public IActionResult UpdateAuthor([FromBody] UpdateAuthorDto updateAuthorDto)
        {
            var updateAuthor = _authorService.UpdateAuthor(updateAuthorDto);

            if (updateAuthor == null)
            {
                return(NotFound("Author with those credentials does not exist."));
            }

            return(Ok(updateAuthor));
        }
예제 #7
0
        public async Task UpdateAsync(Guid id, UpdateAuthorDto input)
        {
            var author = await _authorRepository.GetAsync(id);

            if (author.Name != input.Name)
            {
                await _authorManager.ChangeNameAsync(author, input.Name);
            }

            author.BirthDate = input.BirthDate;
            author.ShortBio  = input.ShortBio;
        }
예제 #8
0
        public async Task <IActionResult> UpdateAsync([FromRoute] int id, [FromBody] UpdateAuthorDto updateItem, CancellationToken cancellationToken)
        {
            if (!await this._authorService.ExistsAsync(id))
            {
                return(StatusCode(StatusCodes.Status404NotFound));
            }

            if (await this._authorService.UpdateAsync(id, updateItem, cancellationToken))
            {
                return(StatusCode(StatusCodes.Status200OK));
            }

            return(StatusCode(StatusCodes.Status400BadRequest));
        }
예제 #9
0
        public AuthorDto UpdateAuthor(UpdateAuthorDto updateAuthorDto)
        {
            var updateAuthor = _authorRepository.UpdateAuthor
                                   (updateAuthorDto.AuthorDto.FirstName, updateAuthorDto.AuthorDto.LastName,
                                   updateAuthorDto.NewFirstName, updateAuthorDto.NewLastName);

            if (updateAuthor == null)
            {
                return(null);
            }

            return(new AuthorDto {
                FirstName = updateAuthor.FirstName, LastName = updateAuthor.LastName
            });
        }
예제 #10
0
        public async Task <ActionResult> Update(int id, [FromBody] UpdateAuthorDto updateAuthorDto)
        {
            var author = await _context.Authors.FindAsync(id);

            if (author == null)
            {
                return(NotFound());
            }

            author.id   = id;
            author.name = updateAuthorDto.name;
            _context.Entry(author).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            await _context.SaveChangesAsync();

            return(NoContent());
        }
예제 #11
0
        public async Task UpdateAuthorAsync_ShouldReturnExpectedAuthor()
        {
            // given (arrange)
            DateTime updateTime = DateTime.UtcNow;
            DateTime createTime = updateTime.AddDays(-5);

            Filler <UpdateAuthorDto> authorFiller = new Filler <UpdateAuthorDto>();

            authorFiller.Setup()
            .OnProperty(x => x.ContactEmail)
            .Use(new EmailAddresses(".com"))
            .OnProperty(x => x.ContactPhoneNumber)
            .Use("555-555-5555");

            UpdateAuthorDto authorToUpdateDto = authorFiller.Create();

            authorToUpdateDto.Id = 1;

            Author authorToUpdate = this.mapper.Map <Author>(authorToUpdateDto);

            authorToUpdate.DateCreated = createTime;
            authorToUpdate.DateUpdated = updateTime;

            Author databaseAuthor = this.mapper.Map <Author>(authorToUpdate);

            databaseAuthor.DateUpdated = updateTime;

            this.appDbContextMock.Setup(db =>
                                        db.SelectAuthorByIdAsync(authorToUpdateDto.Id))
            .ReturnsAsync(authorToUpdate);

            this.appDbContextMock.Setup(db =>
                                        db.UpdateAuthorAsync(It.IsAny <Author>()))
            .ReturnsAsync(databaseAuthor);

            // when (act)
            Author actualAuthor = await subject.UpdateAuthorAsync(authorToUpdateDto);

            // then (assert)
            actualAuthor.Should().BeEquivalentTo(databaseAuthor);
            Assert.Equal(actualAuthor.DateUpdated, updateTime);
            appDbContextMock.Verify(db => db.SelectAuthorByIdAsync(actualAuthor.Id), Times.Once);
            appDbContextMock.Verify(db => db.UpdateAuthorAsync(It.IsAny <Author>()), Times.Once);
            appDbContextMock.VerifyNoOtherCalls();
        }
예제 #12
0
        public async Task <IActionResult> UpdateAuthor(UpdateAuthorDto updateAuthorDto)
        {
            var author = mapper.Map <Author>(updateAuthorDto);
            var result = await courseLibraryService.UpdateAuthor(author);

            if (result.Success)
            {
                var successOperation = result as SuccessOperationResult <Author>;
                var authorDto        = mapper.Map <AuthorDto>(successOperation.Result);
                return(Ok(new SuccessOperationResult <AuthorDto>
                {
                    Code = successOperation.Code,
                    Result = authorDto
                }));
            }
            var failedOperation = result as FailedOperationResult <Author>;

            return(Ok(failedOperation));
        }
예제 #13
0
        public async Task <IActionResult> UpdateAuthorAsync(int authourId, UpdateAuthorDto authorDto)
        {
            var author = await repositoryWrapper.Author.GetByIdAsync(authourId);

            if (author == null)
            {
                return(NotFound());
            }
            mapper.Map(authorDto, author);

            repositoryWrapper.Author.Update(author);
            var result = await repositoryWrapper.Author.SaveAsync();

            if (result)
            {
                return(NoContent());
            }
            return(NotFound());
        }
        public void Execute(UpdateAuthorDto request)
        {
            _validator.ValidateAndThrow(request);
            var author = _context.Authors.Find(request.Id);

            if (author == null)
            {
                throw new EntityNotFOundException(request.Id, typeof(Author));
            }
            if (request.FirstName == null && request.LastName == null)
            {
                request.FirstName = author.FirstName;
                request.LastName  = author.LastName;
            }

            if (request.FirstName == null && request.LastName != null)
            {
                request.FirstName = author.FirstName;
            }

            if (request.FirstName != null && request.LastName == null)
            {
                request.LastName = author.LastName;
            }
            var birthPlace = author.BirthPlace;
            var birth      = author.Birth;

            author.FirstName  = request.FirstName;
            author.LastName   = request.LastName;
            author.BirthPlace = request.BirthPlace ?? birthPlace;
            author.Birth      = request.Birth ?? birth;

            //var birthPlace = actor.BirthPlace;


            // actor.BirthPlace = request.BirthPlace ?? birthPlace;

            _context.SaveChanges();
        }
예제 #15
0
        public async Task <IActionResult> Update([FromBody] UpdateAuthorDto payload, Guid id, Guid AuthorId)
        {
            ApiResponse <UpdateAuthorDto> response = new ApiResponse <UpdateAuthorDto>();

            try
            {
                if (!response.Errors.Any())
                {
                    var userFromRepo = await _userSrv.GetUser(id);

                    if (userFromRepo == null)
                    {
                        return(BadRequest(new { errorList = "Invalid Author Id" }));
                    }
                    var currentUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
                    if (currentUserId != userFromRepo.Email)
                    {
                        return(BadRequest(new { errorList = "UnAuthorized" }));
                    }
                    (List <ValidationResult> Result, UpdateAuthorDto Author)errorResult = await _authorServ.UpdateAuthor(payload, AuthorId);

                    if (errorResult.Result.Any())
                    {
                        return(BadRequest(new { errorList = $"{errorResult.Result.FirstOrDefault().ErrorMessage}" }));
                    }
                    else
                    {
                        response.Code        = ApiResponseCodes.OK;
                        response.Description = $"Author Updated Successfully.";
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
            return(Ok(response));
        }
        public async Task UpdateAuthorAsync_ShouldThrowExceptionForInvalidDataAnnotationRequirement()
        {
            // given (arrange)
            Filler <UpdateAuthorDto> authorFiller = new Filler <UpdateAuthorDto>();

            UpdateAuthorDto invalidAuthorToUpdateDto = authorFiller.Create();

            Author invalidAuthorToUpdate = this.mapper.Map <Author>(invalidAuthorToUpdateDto);

            // Object filler will create an invalid email address by default, so we'll use that //

            this.appDbContextMock.Setup(db =>
                                        db.SelectAuthorByIdAsync(invalidAuthorToUpdateDto.Id))
            .ReturnsAsync(invalidAuthorToUpdate);

            // when (act)
            var actualAuthorTask = subject.UpdateAuthorAsync(invalidAuthorToUpdateDto);

            // then (assert)
            await Assert.ThrowsAsync <ValidationException>(() => actualAuthorTask);

            appDbContextMock.Verify(db => db.SelectAuthorByIdAsync(invalidAuthorToUpdateDto.Id), Times.Once);
            appDbContextMock.VerifyNoOtherCalls();
        }
예제 #17
0
 public Authors()
 {
     NewAuthor     = new CreateAuthorDto();
     EditingAuthor = new UpdateAuthorDto();
 }
예제 #18
0
 public Task UpdateAsync(Guid id, UpdateAuthorDto input)
 {
     return(_authorAppService.UpdateAsync(id, input));
 }
예제 #19
0
 public async Task <ServiceResult> Update([FromBody] UpdateAuthorDto item) => await _authorService.Update(item);
예제 #20
0
        public async Task <(List <ValidationResult> Result, UpdateAuthorDto Author)> UpdateAuthor(UpdateAuthorDto vm, Guid author_Id)
        {
            results.Clear();
            try
            {
                var author = this.GetAll(1, 1, a => a.Id, a => a.Id == author_Id, OrderBy.Descending).FirstOrDefault();
                if (author == null)
                {
                    results.Add(new ValidationResult("Author couldn't be found to complete update operation."));
                    return(results, null);
                }
                author.AuthorName = vm.Name;
                author.IsDeleted  = false;

                bool isValid = Validator.TryValidateObject(author, new ValidationContext(author, null, null),
                                                           results, false);

                if (!isValid || results.Count > 0)
                {
                    return(results, null);
                }

                author.ModifiedOn = DateTime.Now.GetDateUtcNow();

                this.UnitOfWork.BeginTransaction();
                await this.UpdateAsync(author);

                await this.UnitOfWork.CommitAsync();

                return(results, vm);
            }
            catch (Exception ex)
            {
                results.Add(new ValidationResult($"Author couldn't be updated! \n {ex.Message}"));
            }
            return(results, vm);
        }
예제 #21
0
        public async Task <IActionResult> Update(UpdateAuthorDto updateAuthor)
        {
            await _authorService.UpdateAuthorAsync(updateAuthor);

            return(NoContent());
        }