Exemplo n.º 1
0
        public VotingItemResponse CreateVotingItem(VotingItemCreationRequest request, Guid UserID)
        {
            if (request.DueDate < DateTime.Now)
            {
                throw new BusinessLogicException(HttpStatusCode.BadRequest, ResponseCode.VOTING_ITEM_DUE_DATE_NOT_VALID.ToString());
            }
            Users user = dbContext.Users.Find(UserID);

            if (user == null)
            {
                throw new BusinessLogicException(HttpStatusCode.BadRequest, ResponseCode.USER_NOT_EXIST.ToString());
            }

            using (IDbContextTransaction transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    VotingItems votingItem = new VotingItems();
                    votingItem.VotingItemId    = Guid.NewGuid();
                    votingItem.Name            = request.Name;
                    votingItem.Description     = request.Description;
                    votingItem.CreatorUserId   = UserID;
                    votingItem.SupportersCount = 0;
                    votingItem.DueDate         = request.DueDate;
                    votingItem.CreatedDate     = DateTime.Now;
                    votingItem.CreatedBy       = user.Email;
                    votingItem.UpdatedDate     = DateTime.Now;
                    votingItem.UpdatedBy       = user.Email;

                    List <VotingCategories> createdVotingCategories = new List <VotingCategories>();
                    List <Guid>             listCategories          = new List <Guid>();
                    foreach (Guid id in request.Categories)
                    {
                        Categories categories = dbContext.Categories.Find(id);
                        if (categories != null)
                        {
                            VotingCategories votingCategories = new VotingCategories();
                            votingCategories.VotingCategoryId = Guid.NewGuid();
                            votingCategories.CategoryId       = categories.CategoryId;
                            votingCategories.VotingItemId     = votingItem.VotingItemId;
                            votingCategories.CreatorUserId    = user.UserId;
                            votingCategories.CreatedDate      = DateTime.Now;
                            votingCategories.CreatedBy        = user.Email;
                            votingCategories.UpdatedDate      = DateTime.Now;
                            votingCategories.UpdatedBy        = user.Email;
                            createdVotingCategories.Add(votingCategories);
                            listCategories.Add(categories.CategoryId);
                        }
                    }

                    dbContext.VotingItems.Add(votingItem);
                    dbContext.VotingCategories.AddRange(createdVotingCategories);
                    dbContext.SaveChanges();
                    transaction.Commit();

                    List <Categories> categoriesList = dbContext.Categories.Where(cat => listCategories.Contains(cat.CategoryId)).ToList();

                    return(constructResponse(votingItem, categoriesList));
                }
                catch
                {
                    transaction.Rollback();
                    throw new BusinessLogicException(HttpStatusCode.InternalServerError, ResponseCode.FAILED_TO_CREATED_DATA.ToString());
                }
            }
        }
Exemplo n.º 2
0
        public VotingItemResponse UpdateVotingItem(VotingItemUpdateRequest request, Guid UserID)
        {
            Users user = dbContext.Users.Find(UserID);

            if (user == null)
            {
                throw new BusinessLogicException(HttpStatusCode.BadRequest, ResponseCode.USER_NOT_EXIST.ToString());
            }

            VotingItems votingItem = dbContext.VotingItems
                                     .Where(vi => vi.VotingItemId.Equals(request.VotingItemID) && vi.CreatorUserId.Equals(UserID))
                                     .FirstOrDefault();

            if (votingItem == null)
            {
                throw new BusinessLogicException(HttpStatusCode.BadRequest, ResponseCode.DATA_NOT_EXIST.ToString());
            }

            using (IDbContextTransaction transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    votingItem.Name        = request.Name;
                    votingItem.Description = request.Description;
                    votingItem.DueDate     = request.DueDate;
                    votingItem.UpdatedDate = DateTime.Now;
                    votingItem.UpdatedBy   = user.Email;

                    List <VotingCategories> oldVotingCategories = dbContext.VotingCategories
                                                                  .Where(vc => vc.VotingItemId.Equals(request.VotingItemID) && vc.CreatorUserId.Equals(UserID))
                                                                  .ToList();

                    List <VotingCategories> createdVotingCategories = new List <VotingCategories>();
                    List <Guid>             listCategories          = new List <Guid>();
                    foreach (Guid id in request.Categories)
                    {
                        Categories categories = dbContext.Categories.Find(id);
                        if (categories != null)
                        {
                            VotingCategories votingCategories = new VotingCategories();
                            votingCategories.VotingCategoryId = Guid.NewGuid();
                            votingCategories.CategoryId       = categories.CategoryId;
                            votingCategories.VotingItemId     = votingItem.VotingItemId;
                            votingCategories.CreatorUserId    = user.UserId;
                            votingCategories.CreatedDate      = DateTime.Now;
                            votingCategories.CreatedBy        = user.Email;
                            votingCategories.UpdatedDate      = DateTime.Now;
                            votingCategories.UpdatedBy        = user.Email;
                            createdVotingCategories.Add(votingCategories);
                            listCategories.Add(categories.CategoryId);
                        }
                    }

                    dbContext.VotingCategories.RemoveRange(oldVotingCategories);
                    dbContext.VotingCategories.AddRange(createdVotingCategories);
                    dbContext.VotingItems.Update(votingItem);
                    dbContext.SaveChanges();
                    transaction.Commit();

                    List <Categories> categoriesList = dbContext.Categories.Where(cat => listCategories.Contains(cat.CategoryId)).ToList();

                    return(constructResponse(votingItem, categoriesList));
                }
                catch
                {
                    transaction.Rollback();
                    throw new BusinessLogicException(HttpStatusCode.InternalServerError, ResponseCode.FAILED_TO_UPDATE_DATA.ToString());
                }
            }
        }