Пример #1
0
        public async Task <IActionResult> AddPhotoToProfile(int userId,
                                                            [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            User user = await _repo.GetUser(userId);

            if (user.ProfilePhoto != null)
            {
                Photo trashPhoto = await _repo.GetPhotoById(user.ProfilePhoto.Id);

                _repo.Delete(trashPhoto);
            }

            photoForCreationDto.Width  = 500;
            photoForCreationDto.Height = 500;
            photoForCreationDto.UserId = userId;
            photoForCreationDto.IsMain = true;

            Photo photo = UploadPhotoToCloudinary(photoForCreationDto);

            _repo.Add(photo);

            if (await _repo.SaveAllAsync())
            {
                PhotoForReturnDto photoForReturnDto = _mapper.Map <PhotoForReturnDto>(photo);

                return(Ok(photoForReturnDto));
            }

            return(BadRequest("Adding photo to user's profile could not be possible"));
        }
Пример #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."));
        }
Пример #3
0
        public async Task <IActionResult> SaveOffer([FromQuery] int userId,
                                                    OfferForRegisterDto offerForRegisterDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            Offer newOffer = _mapper.Map <Offer>(offerForRegisterDto);

            _repo.Add(newOffer);

            if (await _repo.SaveAllAsync())
            {
                OfferForReturnDto offerForReturnDto = _mapper.Map <OfferForReturnDto>(newOffer);

                return(Ok(offerForReturnDto));
            }

            return(BadRequest("It's not possible saving offer"));
        }