public async Task <ActionResult> Put(int id, [FromBody] UpdateAuctionResource auction) { if (!(await _authorizationService.AuthorizeAsync(User, id, "ModeratorOfAuction")).Succeeded) { return(Forbid()); } await _auctionService.UpdateAuction(id, auction); return(NoContent()); }
public async Task <IActionResult> Put(int id, UpdateAuctionResource model, CancellationToken cancellationToken) { if (id <= 0) { throw new ArgumentException("Negative id exception"); } await this.cache.RemoveAsync("AllHotels", cancellationToken); var entity = await this.context.Auctions.FindAsync(id); if (entity == null) { return(this.NotFound()); } entity.UpdateWith(model); this.context.Auctions.Update(entity); await this.context.SaveChangesAsync(cancellationToken); return(this.NoContent()); }
public async Task UpdateAuction(int auctionToBeUpdatedId, UpdateAuctionResource auction) { var validator = new UpdateAuctionResourceValidator(); var validationResult = await validator.ValidateAsync(auction); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } var actualAuction = await _unitOfWork.Auctions.GetByIdAsync(auctionToBeUpdatedId); if (actualAuction == null) { throw ExceptionBuilder.Create("Auction with provided Id does not exist"); } actualAuction.Name = (!string.IsNullOrEmpty(auction.Name)) ? auction.Name : actualAuction.Name; actualAuction.Description = (!string.IsNullOrEmpty(auction.Description)) ? auction.Description : actualAuction.Description; actualAuction.IsActive = (auction.IsActive.HasValue) ? (bool)auction.IsActive : actualAuction.IsActive; await _unitOfWork.CommitAsync(); }
public static void UpdateWith(this Auction auction, UpdateAuctionResource model) { auction.EndDate = model.EndDate; auction.Name = model.Name; }