Exemplo n.º 1
0
        public WishDto Delete(int id)
        {
            var entity = DatabaseContext.Wishes.Find(id);

            if (entity != null)
            {
                entity.Show = false;

                WishDto dto = new WishDto()
                {
                    WishId   = entity.WishId,
                    Name     = entity.Name,
                    MaxPrice = entity.MaxPrice,
                    ExtraPay = entity.ExtraPay,
                    Show     = entity.Show,

                    MadeById = entity.MadeById,
                    MadeOn   = entity.MadeOn,

                    GrantedById = entity.GrantedById,
                    GrantedOn   = entity.GrantedOn,
                };

                DatabaseContext.SaveChanges();

                return(dto);
            }
            else
            {
                throw (new KeyNotFoundException());
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddWish([FromBody] WishDto payload)
        {
            var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.SerialNumber)?.Value;
            await _achievementService.AddWishAsync(userId, payload);

            return(Ok("Wish has been added!"));
        }
Exemplo n.º 3
0
        public WishDto[] GetCollection()
        {
            List <WishEntity> wishesList  = DatabaseContext.Wishes.ToList();
            List <WishDto>    wishDtoList = new List <WishDto>();

            foreach (WishEntity entity in wishesList)
            {
                if (entity != null)
                {
                    WishDto dto = new WishDto()
                    {
                        WishId   = entity.WishId,
                        Name     = entity.Name,
                        MaxPrice = entity.MaxPrice,
                        ExtraPay = entity.ExtraPay,
                        Show     = entity.Show,

                        MadeById = entity.MadeById,
                        MadeOn   = entity.MadeOn,

                        GrantedById = entity.GrantedById,
                        GrantedOn   = entity.GrantedOn,
                    };
                    wishDtoList.Add(dto);
                }
            }
            return(wishDtoList.ToArray());
        }
Exemplo n.º 4
0
        public WishDto GetById(int id)
        {
            var entity = DatabaseContext.Wishes.Find(id);

            if (entity != null)
            {
                WishDto dto = new WishDto()
                {
                    WishId   = entity.WishId,
                    Name     = entity.Name,
                    MaxPrice = entity.MaxPrice,
                    ExtraPay = entity.ExtraPay,
                    Show     = entity.Show,

                    MadeById = entity.MadeById,
                    MadeOn   = entity.MadeOn,

                    GrantedById = entity.GrantedById,
                    GrantedOn   = entity.GrantedOn,
                };
                return(dto);
            }
            else
            {
                //  Use Key not found Exception for when a resource is not found
                throw new KeyNotFoundException();
            }
        }
Exemplo n.º 5
0
        public WishDto Update(int id)
        {
            WishEntity entity = DatabaseContext.Wishes.Find(id);

            if (entity != null)
            {
                DatabaseContext.Entry(entity).State = System.Data.Entity.EntityState.Modified;

                WishDto dto = new WishDto()
                {
                    WishId = entity.WishId,
                    //UserId = entity.UserId,
                    ExtraPay  = entity.ExtraPay,
                    MaxPrice  = entity.MaxPrice,
                    GrantedOn = entity.GrantedOn,
                    Name      = entity.Name
                };

                DatabaseContext.SaveChanges();

                return(dto);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
        private static WishDto ConvertToDto(Wish wish)
        {
            var dto = new WishDto()
            {
                Id      = wish.Id,
                Country = new CountryDto()
                {
                    Code = wish.Country1.Id, Name = wish.Country1.Name
                },
                Description = wish.Description,
                Benefit     = wish.Benefit,
                ImageUrl    = wish.ImageUrl,
                City        = wish.City,
                FromDate    = wish.FromDate,
                ToDate      = wish.ToDate,
                Location    = wish.Location,
                Name        = wish.Name,
                Status      = new StatusDto()
                {
                    Code = wish.GiftWishStatus.Code, Status = wish.GiftWishStatus.Status
                },
                Emergency    = wish.Emergency,
                Category     = wish.WishCategory.Name,
                Participants =
                    wish.WishGiftLinks.Select(
                        x =>
                        new ParticipantDto()
                {
                    FirstName = x.Gift.User.Profile.FirstName,
                    Id        = x.Gift.User.Id,
                    LastName  = x.Gift.User.Profile.LastName,
                    Target    = new TargetLinkItem()
                    {
                        TargetId = x.Gift.Id, Type = "gift", TargetName = x.Gift.Name
                    }
                }),
                Creator =
                    new CreatorDto()
                {
                    AvatarUrl       = wish.User.Profile.AvatarUrl,
                    CreatorId       = wish.User.Id,
                    FirstName       = wish.User.Profile.FirstName,
                    LastName        = wish.User.Profile.LastName,
                    FavoriteContact =
                        wish.User.Profile.Contacts.Where(x => x.MainContact)
                        .Select(
                            x =>
                            new ContactDto()
                    {
                        Name        = x.ContactType.Name,
                        Value       = x.Value,
                        MainContact = x.MainContact
                    })
                        .FirstOrDefault()
                },
            };

            return(dto);
        }
Exemplo n.º 7
0
        public async Task AddUpdateAsync(WishDto wish)
        {
            var wishEntity = _mapper.Map <WishDto, Wish>(wish);

            await _uow.Wishes.AddAsync(wishEntity);

            await _uow.SaveAsync();
        }
        //Добавление вишеа
        public async Task <long> AddWish(long userId, WishDto wish)
        {
            var newWish = new Wish();

            newWish = UpdateEfModelFromDto(newWish, wish, userId);
            base.Insert(newWish);
            base.Save();
            return(newWish.Id);
        }
Exemplo n.º 9
0
        public void Create_WhenWishIsEmpty_ThenThrowValidExeption()
        {
            // Arrange
            var wish = new WishDto();

            var service = new WishListService(_unitOfWorkFake, _mapper, _validator);

            // Act - Assert
            Assert.Throws <ValidationException>(() => service.Create(wish));
        }
        public async Task <WishDto> UpdateWish(long userId, WishDto updatedWish)
        {
            var originalWish = Db.Set <Wish>().Find(updatedWish.Id);

            originalWish = UpdateEfModelFromDto(originalWish, updatedWish, userId);
            base.Update(originalWish);
            await Db.SaveChangesAsync();

            return(ConvertToDto(originalWish));
        }
Exemplo n.º 11
0
        public void Find_WhenFindUnknownWish_ThenReturnEmpty()
        {
            // Arrange
            var wish = new WishDto();

            var service = new WishListService(_unitOfWorkFake, _mapper, _validator);

            // Act
            var result = service.Find(string.Empty);

            // Assert
            Assert.IsEmpty(result);
        }
Exemplo n.º 12
0
        public async Task AddWishAsync(string userId, WishDto payload)
        {
            var achievement = new Achievement
            {
                Name    = payload.Name,
                Price   = payload.Price,
                UserId  = userId,
                Reached = false
            };

            UnitOfWork.Achievements.Add(achievement);
            await UnitOfWork.SaveChangesAsync();
        }
Exemplo n.º 13
0
        public async Task <ServiceResponse <WishlistDto> > AddToWishlist(WishDto newWish)
        {
            ServiceResponse <WishlistDto> response = new ServiceResponse <WishlistDto>();
            var wishlist = await _context.Wishlists.FirstOrDefaultAsync(w => w.UserId == GetUserId());

            if (wishlist == null)
            {
                wishlist = new Wishlist {
                    UserId = GetUserId()
                };
            }

            Wish wish = new Wish {
                ProductId  = newWish.ProductId,
                Category   = newWish.Category,
                Price      = newWish.Price,
                PictureUrl = newWish.PictureUrl,
                OPrice     = newWish.OPrice,
                Wishlist   = wishlist
            };

            IQueryable <Wish> wishesIQ = from i in _context.Wishes
                                         select i;

            var count = wishesIQ.Where(i => i.WishlistId == wishlist.Id).Count();

            if (_context.Wishes.Any(i => i.WishlistId == wishlist.Id && i.ProductId == wish.ProductId))
            {
                await RemoveSpecificWish(wish.ProductId);

                response.Message = "removed";
            }

            else if (count >= 5)
            {
                response.Message = "You have reached the maximum of items that you can add to your basket!";
                response.Success = false;
            }

            else
            {
                await _context.Wishes.AddAsync(wish);

                await _context.SaveChangesAsync();

                response.Message = "Done";
            }

            response.Data = _mapper.Map <WishlistDto>(wishlist);
            return(response);
        }
Exemplo n.º 14
0
 public IHttpActionResult Delete(int id)
 {
     if (ModelState.IsValid)
     {
         using (logic)
         {
             WishDto dto = logic.Delete(id);
             return(Ok(dto));
         }
     }
     else
     {
         return(BadRequest(ModelState));
     }
 }
Exemplo n.º 15
0
        /// <inheritdoc/>
        public void Update(Guid id, WishDto dto)
        {
            var wish = _mapper.Map <WishDto, Wish>(dto);

            wish.Id = id;

            var validationResult = _validator.Validate(wish);

            if (validationResult.IsValid)
            {
                _uow.WishListRepository.Update(wish);
            }
            else
            {
                throw new ValidationException(validationResult.Errors);
            }
        }
Exemplo n.º 16
0
        public async Task <IActionResult> AddWish([FromBody] WishDto wish)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Invalid model object"));
                }

                await _wishService.AddWishAsync(wish);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
Exemplo n.º 17
0
        public void Create(WishDto dto)
        {
            WishEntity entity = new WishEntity()
            {
                ExtraPay    = dto.ExtraPay,
                MadeOn      = (DateTime)dto.MadeOn,
                MadeById    = dto.MadeById,
                GrantedById = dto.GrantedById,
                GrantedOn   = dto.GrantedOn,
                Name        = dto.Name,
                MaxPrice    = dto.MaxPrice
            };

            DatabaseContext.Wishes.Add(entity);

            DatabaseContext.SaveChanges();

            dto.WishId = entity.WishId;
        }
Exemplo n.º 18
0
        public void Find_WhenFindWish_ThenReturnNeeded()
        {
            // Arrange
            var wishDto = new WishDto();
            var wish    = new Wish();

            var service = new WishListService(_unitOfWorkFake, _mapper, _validator);

            A.CallTo(() => _unitOfWorkFake.WishListRepository.Find(A <Func <Wish, bool> > ._))
            .Returns(new List <Wish> {
                wish
            });

            // Act
            var returnedWish = service.Find(wish.Id.ToString());

            // Assert
            Assert.AreEqual(wishDto.Id, returnedWish[0].Id);
        }
Exemplo n.º 19
0
 public IHttpActionResult Get(int id)
 {
     if (ModelState.IsValid)
     {
         using (logic)
         {
             WishDto dto = logic.GetById(id);
             dto.User = new LinkDto($"api/Wishes/{id}/UserId", "user", "GET");
             dto.Links.Add(new LinkDto($"api/Wishes/{id}", "self", "GET"));
             dto.Links.Add(new LinkDto($"api/Wishes/{id}", "create-wish", "POST"));
             dto.Links.Add(new LinkDto($"api/Wishes/{id}", "update-wish", "PUT"));
             dto.Links.Add(new LinkDto($"api/Wishes/{id}", "delete-wish", "DELETE"));
             return(Ok(dto));
         }
     }
     else
     {
         return(BadRequest(ModelState));
     }
 }
Exemplo n.º 20
0
        public IHttpActionResult Create(WishDto dto)
        {
            if (ModelState.IsValid)
            {
                using (logic)
                {
                    dto.Show      = true;
                    dto.MadeById  = Request.GetOwinContext().Authentication.User.Identity.GetUserId();
                    dto.MadeOn    = DateTime.Now;
                    dto.GrantedOn = null;

                    logic.Create(dto);
                    return(Ok(dto));
                }
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
        private Wish UpdateEfModelFromDto(Wish wish, WishDto dto, long userId)
        {
            var category = Db.Set <WishCategory>().FirstOrDefault(x => x.Name == dto.Category);
            var country  = Db.Set <Country>().FirstOrDefault(x => x.Id == dto.Country.Code);
            var status   = Db.Set <GiftWishStatus>().FirstOrDefault(x => x.Code.Equals(0));

            wish.Benefit        = dto.Benefit;
            wish.WishCategory   = category;
            wish.City           = dto.City;
            wish.Country1       = country;
            wish.Description    = dto.Description;
            wish.FromDate       = dto.FromDate;
            wish.ToDate         = dto.ToDate;
            wish.ImageUrl       = dto.ImageUrl;
            wish.UserId         = userId;
            wish.Emergency      = dto.Emergency;
            wish.Name           = dto.Name;
            wish.Location       = dto.Location;
            wish.GiftWishStatus = status;
            wish.CreatedTime    = DateTime.Now;
            return(wish);
        }
Exemplo n.º 22
0
        public WishDto[] GetCollectionByUserId(string userId)
        {
            WishDto[] result;


            UserEntity user = DatabaseContext.Users.Find(userId);

            if (user != null)
            {
                List <WishDto> list = new List <WishDto>();
                foreach (var entity in user.MadeWishes)
                {
                    WishDto dto = new WishDto()
                    {
                        WishId   = entity.WishId,
                        Name     = entity.Name,
                        MaxPrice = entity.MaxPrice,
                        ExtraPay = entity.ExtraPay,
                        Show     = entity.Show,

                        MadeById = entity.MadeById,
                        MadeOn   = entity.MadeOn,

                        GrantedById = entity.GrantedById,
                        GrantedOn   = entity.GrantedOn,
                    };
                }
                result = list.ToArray();
            }
            else
            {
                throw new KeyNotFoundException("Requested user not found");
            }

            return(result);
        }
Exemplo n.º 23
0
 public async Task <IActionResult> AddToWishlist(WishDto newWish)
 {
     return(Ok(await _repo.AddToWishlist(newWish)));
 }