Beispiel #1
0
        public static List <OfferForCartDto> GetOfferCartDtoList(
            List <UserOffer> userOffers,
            IMapper mapper)
        {
            List <OfferForCartDto> offerForCartDtos = new List <OfferForCartDto>();

            userOffers.ForEach(uo => {
                OfferForCartDto offerDto = mapper.Map <OfferForCartDto>(uo.Offer);
                offerDto.Amount          = uo.Amount;

                offerForCartDtos.Add(offerDto);
            });

            return(offerForCartDtos);
        }
Beispiel #2
0
        public async Task <IActionResult> AddOfferToCartForUser(int userId, OfferForCartDto offerForCartDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            List <Offer> offersFromUser = await _repo.GetOffersCart(userId);

            if (!offersFromUser.Exists(off => off.Id == offerForCartDto.OfferId))
            {
                UserOffer userOffer = new UserOffer
                {
                    Amount  = offerForCartDto.Amount,
                    UserId  = userId,
                    OfferId = offerForCartDto.OfferId
                };

                _repo.Add(userOffer);
            }
            else
            {
                List <UserOffer> list = await _repo.GetUserOffersCart(userId);

                var userOffer = list.Find(uo => uo.OfferId == offerForCartDto.OfferId);
                userOffer.Amount = offerForCartDto.Amount;
                _repo.Update(userOffer);
            }

            if (await _repo.SaveAllAsync())
            {
                List <UserOffer> userOffers = await _repo.GetUserOffersCart(userId);

                List <OfferForCartDto> offerForCartDtos;

                offerForCartDtos = Utils.GetOfferCartDtoList(userOffers, _mapper);
                return(Ok(offerForCartDtos));
            }

            return(BadRequest("It wasn't possible adding offer to the cart."));
        }
Beispiel #3
0
        public async Task <IActionResult> RemoveOfferFromCartForUser(int userId, OfferForCartDto offerForCartDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            List <Offer> offersFromUser = await _repo.GetOffersCart(userId);

            if (!offersFromUser.Exists(off => off.Id == offerForCartDto.OfferId))
            {
                return(BadRequest("Offer does not exist in user's cart."));
            }

            List <UserOffer> list = await _repo.GetUserOffersCart(userId);

            var userOffer = list.Find(uo => uo.OfferId == offerForCartDto.OfferId);

            if (offerForCartDto.Amount == 0)
            {
                _repo.Delete(userOffer);
            }
            else
            {
                userOffer.Amount = offerForCartDto.Amount;
                _repo.Update(userOffer);
            }

            if (await _repo.SaveAllAsync())
            {
                List <UserOffer> userOffers = await _repo.GetUserOffersCart(userId);

                List <OfferForCartDto> offerForCartDtos;

                offerForCartDtos = Utils.GetOfferCartDtoList(userOffers, _mapper);
                return(Ok(offerForCartDtos));
            }

            return(BadRequest("It wasn't possible removing offer from cart."));
        }