public async Task <IActionResult> AddProduct([FromBody] ProductRequest productRequest) { if (productRequest is null) { return(UnprocessableEntity(Constants.ErrorMessages.UnprocessableEntity)); } var descriptionErrorMessage = VerifyProductDescription(productRequest.ShortDescription, productRequest.LongDescription); if (!string.IsNullOrWhiteSpace(descriptionErrorMessage)) { return(BadRequest(descriptionErrorMessage)); } Product product = productRequest.MapToEntity(); bool parsed = int.TryParse(User.Identity.Name, out int userId); if (!parsed) { throw new ApplicationException(Constants.ErrorMessages.UnreachableUserId); } product.OwnerId = userId; var entry = _context.Products.Add(product); try { await _context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception) { return(this.InternalServerError()); } var response = entry.Entity.MapToResponse(); return(Ok(response)); }
public async Task <IActionResult> AddCategory([FromBody] CategoryRequest request) { if (request == null) { return(UnprocessableEntity(Constants.ErrorMessages.UnprocessableEntity)); } if (string.IsNullOrWhiteSpace(request.Name)) { return(BadRequest(Constants.ErrorMessages.EmptyProductTypeName)); } if (_context.Categories.Any(pt => pt.Name == request.Name)) { return(BadRequest(Constants.ErrorMessages.ProductTypeAlreadyExists)); } Category productType = request.MapToEntity(); var entry = _context.Categories.Add(productType); try { await _context.SaveChangesAsync(); } catch (Exception) { return(this.InternalServerError()); } var response = entry.Entity.MapToResponse(); return(Ok(response)); }
public async Task <IActionResult> CreateOffer(OfferRequest request) { if (request is null) { return(UnprocessableEntity(Constants.ErrorMessages.UnprocessableEntity)); } if (!_context.Auctions.Any(a => a.Id == request.AuctionId)) { return(BadRequest(Constants.ErrorMessages.AuctionDoesntExist)); } if (_context.Auctions.Where(a => a.Id == request.AuctionId).Select(a => a.StartDate).FirstOrDefault().Date > DateTime.UtcNow) { return(BadRequest(Constants.ErrorMessages.ToSoon)); } var auctionPrice = 0M; if (_context.Offers.Any(a => a.AuctionId == request.AuctionId)) { auctionPrice = _context.Offers.Where(a => a.AuctionId == request.AuctionId) .OrderByDescending(a => a.Price) .Select(a => a.Price) .FirstOrDefault(); if (request.Price <= auctionPrice) { return(BadRequest(Constants.ErrorMessages.InvalidPriceOffer)); } } else { auctionPrice = _context.Auctions.Where(a => a.Id == request.AuctionId) .Select(a => a.StartingPrice).FirstOrDefault(); if (request.Price < auctionPrice) { return(BadRequest(Constants.ErrorMessages.InvalidPriceOffer)); } } var offer = request.MapToEntity(); bool parsed = int.TryParse(User.Identity.Name, out int userId); if (!parsed) { throw new ApplicationException(Constants.ErrorMessages.UnreachableUserId); } offer.UserId = userId; var entity = _context.Offers.Add(offer); try { await _context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception) { return(this.InternalServerError()); } return(Ok(entity.Entity)); }
public async Task <IActionResult> Register([FromBody] SignUpRequest signUpRequest) { if (signUpRequest == null) { return(UnprocessableEntity(Constants.ErrorMessages.UnprocessableEntity)); } if (!RegisterFieldsAreValid(signUpRequest)) { return(BadRequest(Constants.ErrorMessages.EmptyFields)); } if (await _context.Users.AnyAsync(user => user.Email == signUpRequest.Email)) { return(BadRequest("The given e-mail is associated with an account.")); } User user = signUpRequest.MapToEntity(); _context.Users.Add(user); try { await _context.SaveChangesAsync(); } catch (Exception) { return(this.InternalServerError()); } return(Ok(user)); }
public async Task <IActionResult> Create([FromBody] AuctionRequest auctionRequest) { if (auctionRequest is null) { return(UnprocessableEntity(Constants.ErrorMessages.UnprocessableEntity)); } if (auctionRequest.StartDate < DateTime.UtcNow.Date) { return(BadRequest(Constants.ErrorMessages.StartDateBeforeToday)); } if (auctionRequest.StartingPrice < (int)Metrics.MinimumAllowedValue) { return(BadRequest(Constants.ErrorMessages.MinimumStartPrice)); } if (auctionRequest.DaysActive < (int)Metrics.MinimumAllowedValue) { return(BadRequest(Constants.ErrorMessages.MinimumDaysActive)); } if (_context.Auctions.Any(a => a.ProductId == auctionRequest.ProductId)) { return(BadRequest(Constants.ErrorMessages.AuctionAllreadyExists)); } var auction = auctionRequest.MapToEntity(); bool parsed = int.TryParse(User.Identity.Name, out int userId); if (!parsed) { throw new ApplicationException(Constants.ErrorMessages.UnreachableUserId); } auction.CreatorId = userId; var entity = _context.Auctions.Add(auction); try { await _context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception) { return(this.InternalServerError()); } BackgroundJob.Schedule(() => CloseAuction(auction.Id), TimeSpan.FromDays(auction.DaysActive)); return(Ok(entity.Entity)); }