示例#1
0
        public IActionResult Use(ListToDoUse model)
        {
            model.Items = model.Items.Where(x => x.Deleted == false).ToList();
            if (ModelState.IsValid)
            {
                toDoService.Save(model, User.Identity.Name);
                return(Redirect($"{Constants.ReactAppPath}/{model.DirectoryId}"));
            }

            return(View(model));
        }
示例#2
0
        private void RemoveDeletedItems(ListToDoUse model, int[] validItemIds)
        {
            ///Removing Deleted Items
            var allSentIds = model.Items
                             .Select(x => x.Id).ToArray();
            var deletedItems = validItemIds
                               .Where(x => !allSentIds.Contains(x));
            var toDeleteDbItems = context.ListToDoItems
                                  .Where(x => deletedItems.Contains(x.Id))
                                  .ToArray();

            trackableService.DeleteMany(toDeleteDbItems, DateTime.UtcNow, false);
        }
示例#3
0
        //Verified
        //Regesters Deletion
        //Registers Modifications
        public void Save(ListToDoUse model, string username)
        {
            ListToDo listToDo;

            int[] validItemIds;

            this.VerifyListToSaveBelongsToUser(model, username, out listToDo);

            this.ChangeExistingItems(model, out validItemIds);

            this.trackableService.RegisterModification(listToDo, DateTime.UtcNow, false);

            this.PersistNewItems(model, listToDo);

            this.RemoveDeletedItems(model, validItemIds);

            context.SaveChanges();
        }
示例#4
0
        private void PersistNewItems(ListToDoUse model, ListToDo listToDo)
        {
            ///Adding new items
            var itemsToAdd = model.Items
                             .Where(x => x.Id == 0)
                             .ToArray();

            var dbNewItems = itemsToAdd.Select(x => new ListToDoItem
            {
                ListToDoId = listToDo.Id,
                Comment    = x.Comment,
                Content    = x.Content,
                Status     = x.Status,
                Order      = x.Order,
            })
                             .ToArray();

            context.ListToDoItems.AddRange(dbNewItems);
        }
示例#5
0
        private void VerifyThatToBeChangedIdsBelongToCurrentList(ListToDoUse model, out int[] validItemsIds)
        {
            ///Getting the ids if the items that exest in the database
            validItemsIds = context.ListsTodo
                            .SingleOrDefault(x => x.Id == model.Id)
                            .Items
                            .Select(x => x.Id)
                            .ToArray();

            ///If the user sends id for modification that is not
            ///in existring items return an exception
            foreach (var item in model.Items.Where(x => x.Id > 0))
            {
                if (!validItemsIds.Contains(item.Id))
                {
                    throw new AccessDenied("The items you are trying to modify do not belong to the current list!");
                }
            }
        }
示例#6
0
        private void VerifyListToSaveBelongsToUser(ListToDoUse model, string username, out ListToDo listToDo)
        {
            listToDo = context.ListsTodo.SingleOrDefault(x => x.Id == model.Id);
            if (listToDo == null)
            {
                throw new ItemNotFound("The list you are trying to modify does not exist!");
            }

            var userId = context.Users.SingleOrDefault(x => x.UserName == username)?.Id;

            if (userId == null)
            {
                throw new UserNotFound(username);
            }

            if (listToDo.UserId != userId)
            {
                throw new AccessDenied("The list you are trying to modify does not beling to you!");
            }
        }
示例#7
0
        private void ChangeExistingItems(ListToDoUse model, out int[] validItemIds)
        {
            this.VerifyThatToBeChangedIdsBelongToCurrentList(model, out validItemIds);

            var changedPageModels = model.Items
                                    .Where(x => x.Changed == true && x.Id > 0)
                                    .OrderBy(x => x.Id)
                                    .ToArray();

            var changedModelsIds = changedPageModels
                                   .Select(x => x.Id).ToArray();

            var toBeChangedDbModels = context.ListToDoItems
                                      .Where(x => changedModelsIds.Contains(x.Id))
                                      .OrderBy(x => x.Id)
                                      .ToArray();

            if (changedPageModels.Length != toBeChangedDbModels.Length)
            {
                throw new InternalServerError("Error occurred trying to apply changes to modified listToDo items");
            }

            for (int i = 0; i < changedPageModels.Length; i++)
            {
                if (changedPageModels[i].Id != toBeChangedDbModels[i].Id)
                {
                    throw new InternalServerError("Error occurred trying to apply changes to modified listToDo items");
                }

                toBeChangedDbModels[i].Content = changedPageModels[i].Content;
                toBeChangedDbModels[i].Comment = changedPageModels[i].Comment;
                toBeChangedDbModels[i].Status  = changedPageModels[i].Status;
                toBeChangedDbModels[i].Order   = changedPageModels[i].Order;
            }

            this.trackableService.RegisterModificationMany(toBeChangedDbModels, DateTime.UtcNow, false);
        }