Esempio n. 1
0
        /// <inheritdoc />
        public async Task <AuctionItemModel> UpdateAsync(int id, AuctionItemInputModel model)
        {
            if (!Validator.TryValidateObject(model, new ValidationContext(model), null, true))
            {
                return(null);
            }
            if (!AreAuctionItemDatesValid(model))
            {
                return(null);
            }
            try {
                var auctionItem = await workUnit.AuctionItemRepository.GetAll(false)
                                  .FirstOrDefaultAsync(x => x.Id == id);

                if (auctionItem == null || DateTime.UtcNow >= auctionItem.StartDate)
                {
                    return(null);
                }
                model.Id = id;
                mapper.Map(model, auctionItem);
                auctionItem.ActualCloseDate = auctionItem.PlannedCloseDate;
                await workUnit.SaveAsync();

                AuctionItem result = await workUnit.AuctionItemRepository.GetByIdWithDetailsAsync(id);

                return(mapper.Map <AuctionItemModel>(result));
            } catch (DbUpdateException) {
                return(null);
            } catch (Exception ex) {
                logger.LogError(ex, ExceptionThrownInService);
                throw;
            }
        }
Esempio n. 2
0
        /// <inheritdoc />
        public async Task <AuctionItemInputModel> AddAsync(AuctionItemInputModel model)
        {
            if (!Validator.TryValidateObject(model, new ValidationContext(model), null, true))
            {
                return(null);
            }
            if (!AreAuctionItemDatesValid(model))
            {
                return(null);
            }
            try {
                AuctionItem auctionItem = mapper.Map <AuctionItem>(model);
                auctionItem.ActualCloseDate = auctionItem.PlannedCloseDate;
                await workUnit.AuctionItemRepository.AddAsync(auctionItem);

                await workUnit.SaveAsync();

                model.Id = auctionItem.Id;
                return(model);
            } catch (DbUpdateException) {
                return(null);
            } catch (Exception ex) {
                logger.LogError(ex, ExceptionThrownInService);
                throw;
            }
        }
        public async Task <ActionResult <AuctionItemInputModel> > Add([FromBody] AuctionItemInputModel model)
        {
            if (!User.TryGetUserIdentity(out UserIdentity userIdentity))
            {
                return(StatusCode(500));
            }
            model.SellerId = userIdentity.Id;
            var result = await auctionItemService.AddAsync(model);

            if (result == null)
            {
                return(BadRequest());
            }
            return(result);
        }
Esempio n. 4
0
 private bool AreAuctionItemDatesValid(AuctionItemInputModel model)
 => DateTime.UtcNow < model.StartDate &&
 model.PlannedCloseDate >= model.StartDate.AddMinutes(minAuctionStartToClosingIntervalMinutes);