예제 #1
0
        public async Task <IActionResult> Add(AddTodoViewModel model)
        {
            if (ModelState.IsValid)
            {
                TodoItem newItem = new TodoItem(model.Text, await GetUserId());
                newItem.DateDue = model.DateDue;

                if (!string.IsNullOrEmpty(model.Labels))
                {
                    var lbls = model.Labels.Split(',').Select(tl => tl.Trim().ToLower());
                    foreach (var label in lbls)
                    {
                        if (label.Equals(""))
                        {
                            continue;
                        }

                        var newlbl = new TodoItemLabel(label);
                        var lbl    = _repository.LabelExists(newlbl);
                        if (!newItem.Labels.Contains(lbl))
                        {
                            newItem.Labels.Add(lbl);
                        }
                    }
                }
                _repository.Add(newItem);
                return(RedirectToAction("Index"));
            }
            return(View("Add"));
        }
예제 #2
0
        public async Task <IActionResult> AddNew(AddTodoViewModel todoViewModel)
        {
            if (!ModelState.IsValid)
            {
                todoViewModel.Message = "Text field must not be empty.";
                return(RedirectToAction("Add", todoViewModel));
            }

            var currentUser = await _userManager.GetUserAsync(HttpContext.User);

            var todoItem = new TodoItem(todoViewModel.Text, Guid.Parse(currentUser.Id))
            {
                DateDue = todoViewModel.DateDue
            };

            _repository.Update(todoItem, Guid.Parse(currentUser.Id));

            if (todoViewModel.Labels == null || todoViewModel.Labels.Trim().Length <= 0)
            {
                return(RedirectToAction("Index"));
            }

            var labels = new SortedSet <string>(todoViewModel.Labels.ToLower().Replace(",", " ").Replace("  ", " ").Trim().Split(' ').ToList());

            foreach (var l in labels)
            {
                var label = new TodoItemLabel(l);
                await _labelRepository.Update(label, todoItem);
            }

            return(RedirectToAction("Index"));
        }
예제 #3
0
        public async Task <IActionResult> Add(AddTodoViewModel item)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser applicationUser = await _userManager.GetUserAsync(HttpContext.User);

                TodoItem todo = new TodoItem(item.Text, new Guid(applicationUser.Id))
                {
                    DateDue = item.DateDue
                };
                if (item.Labels != null)
                {
                    string[] labels = item.Labels.Split(',');
                    foreach (var l in labels)
                    {
                        TodoItemLabel todoItemLabel = new TodoItemLabel(l.Trim());
                        todoItemLabel = _repository.AddLabel(todoItemLabel);
                        todo.Labels.Add(todoItemLabel);
                    }
                }
                _repository.Add(todo);
                return(RedirectToAction("Index"));
            }
            return(View());
        }
예제 #4
0
        public async Task <IActionResult> Add(AddTodoViewModel item)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser currentUser = await _userManager.GetUserAsync(HttpContext.User);

                TodoItem todoItem = new TodoItem(item.Text, new Guid(currentUser.Id));
                todoItem.DateDue = item.DateDue;

                string[] splitLabels = item.Labels.Split('|');

                if (splitLabels.Length > 0)
                {
                    foreach (string label in splitLabels)
                    {
                        TodoItemLabel temp = new TodoItemLabel(label.Trim());
                        temp = _repository.ItemLabelCheck(temp);
                        todoItem.Labels.Add(temp);
                        //todoItem.Labels.Add(new TodoItemLabel(label));
                    }
                }
                _repository.Add(todoItem);
                return(RedirectToAction("Index"));
            }
            return(View());
        }
예제 #5
0
        public async Task <IActionResult> Add(AddTodoViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(HttpContext.User);

                TodoItem todoItem = new TodoItem(model.Text, new Guid(user.Id));
                todoItem.DateDue = model.DateDue;
                try
                {
                    _repository.Add(todoItem);
                }
                catch (DuplicateTodoItemException ex)
                {
                    Log.Information(ex.Message);
                }
                String[] labels = model.separateLabels();
                if (labels != null)
                {
                    foreach (string label in labels)
                    {
                        TodoItemLabel todoItemLabel = _repository.AddLabel(label);
                        _repository.AddLabelToTodoItem(todoItemLabel, todoItem);
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View());
        }
예제 #6
0
 public static EditLabelViewModel FromEntity(TodoItemLabel label)
 {
     return(new EditLabelViewModel()
     {
         Id = label.Id,
         Value = label.Value
     });
 }
예제 #7
0
 public void AddLabel(TodoItemLabel label)
 {
     if (_context.TodoItemLabels.Find(label.Id) != null)
         throw new DuplicateTodoItemException
             ($"Duplicate id: {label.Id}");
     _context.TodoItemLabels.Add(label);
     _context.SaveChanges();
 }
예제 #8
0
        public async Task <bool> AddLabelAsync(TodoItemLabel label)
        {
            if (await GetLabelAsync(label.Value) != null)
            {
                return(false);
            }

            _context.TodoLabels.Add(label);
            _context.SaveChanges();
            return(true);
        }
예제 #9
0
        public TodoItem GetItemWithLabels()
        {
            foreach (var s in _labeleString)
            {
                TodoItemLabel itemLabel = new TodoItemLabel(s);
                itemLabel.LabelTodoItems.Add(_item);
                _item.Labels.Add(itemLabel);

                _labels.Add(itemLabel);
            }
            return(_item);
        }
예제 #10
0
        public async Task <bool> UpdateLabelAsync(TodoItemLabel label)
        {
            if (await GetLabelAsync(label.Value) != null)
            {
                return(false);
            }

            Update(label);
            await _context.SaveChangesAsync();

            return(true);
        }
예제 #11
0
        public TodoItemLabel ItemLabelCheck(TodoItemLabel newLabel)
        {
            TodoItemLabel itemLabel =
                _context.TodoItemLabels.Where(label => label.Value == newLabel.Value).FirstOrDefault();

            if (itemLabel == null)
            {
                _context.TodoItemLabels.Add(newLabel);
                _context.SaveChanges();
                return(newLabel);
            }

            return(itemLabel);
        }
예제 #12
0
        public bool Update(TodoItemLabel todoItemLabel)
        {
            bool result = false;

            try
            {
                _unitOfWork.TodoItemLabels.Update(todoItemLabel);
                _unitOfWork.Complete();
                result = true;
            }
            catch (Exception)
            {
                _unitOfWork.Dispose();
            }

            return(result);
        }
예제 #13
0
        public async Task Update(TodoItemLabel label, TodoItem todo)
        {
            using (var db = new TodoDbContext(_connectionString))
            {
                var l = await db.TodoLabel.SingleOrDefaultAsync(t => t.Value.Equals(label.Value));

                var i = await db.TodoItem.Include(t => t.Labels).SingleAsync(t => t.Id.Equals(todo.Id));

                if (l == null)
                {
                    l = label;
                }

                i.Labels.Add(l);

                db.Entry(i).State = EntityState.Modified;
                db.SaveChanges();
            }
        }
예제 #14
0
        public async Task <IActionResult> Add(AddTodoViewModel item)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            var currentUser = await _userManager.GetUserAsync(HttpContext.User);

            var _item = new TodoItem(item.Text, new Guid((currentUser.Id)))
            {
                DateDue = item.DateDue
            };

            if (item.Labels != null)
            {
                string[] labels = item.Labels.Split(',');

                foreach (var i in labels)
                {
                    var todoItemLabel = new TodoItemLabel(i.Trim());
                    _repository.AddLabel(todoItemLabel);
                    var state = false;
                    foreach (var j in _item.Labels)
                    {
                        if (j.Equals(todoItemLabel))
                        {
                            state = true;
                        }
                        break;
                    }
                    if (!state)
                    {
                        _item.Labels.Add(todoItemLabel);
                    }
                }
            }
            await _repository.AddAsync(_item);

            return(RedirectToAction("Index"));
        }
예제 #15
0
        public bool AddLabel(long todoItemId, long labelId)
        {
            bool result = false;

            try
            {
                var todoItemLabel = new TodoItemLabel
                {
                    TodoItemId = todoItemId,
                    LabelId    = labelId
                };

                _unitOfWork.TodoItemLabels.Add(todoItemLabel);

                _unitOfWork.Complete();
                result = true;
            }
            catch (Exception)
            {
                _unitOfWork.Dispose();
            }

            // TODO: Remove debug lines
            // DEBUG START
            var itemLabels = _unitOfWork.TodoItemLabels.GetAll().ToList();

            foreach (var i in itemLabels)
            {
                var itemLabel = itemLabels.FirstOrDefault(il => il.Id == i.Id);
                Debug.WriteLine("TodoService.AddLabel() " + i.Id + " element: \n"
                                + "Id: " + itemLabel.Id + "\n"
                                + "ItemId: " + itemLabel.TodoItemId + "\n"
                                + "LabelId: " + itemLabel.LabelId
                                + "\n----------\n");
            }

            // DEBUG END
            return(result);
        }
예제 #16
0
        public async Task <IActionResult> Labels(LabelsViewModel label)
        {
            if (ModelState.IsValid)
            {
                var labels = await _repository.GetLabels();

                var addLabel = new TodoItemLabel(label.Value)
                {
                    Id             = Guid.NewGuid(),
                    LabelTodoItems = label.LabelTodoItems
                };
                if (labels.Any(dbLabels => dbLabels.Value.Equals(addLabel.Value)))
                {
                    ModelState.AddModelError("ErrorMessage", "Label with the same name already exists!");
                    return(View(label));
                }

                _repository.AddLabel(addLabel);
                return(RedirectToAction("Add"));
            }
            return(View(label));
        }
예제 #17
0
        public async Task <IActionResult> Add(AddTodoViewModel todoModel)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            ApplicationUser usr = await _userManager.GetUserAsync(HttpContext.User);

            TodoItem item = new TodoItem(todoModel.Text, usr.GuidId);

            item.DateCreated = DateTime.Now;
            item.DateDue     = todoModel.DateDue;

            string[] labels = todoModel.labels == null?new string[0] {
            }:todoModel.labels.Split(';');
            item.Labels = new List <TodoItemLabel>();

            foreach (string lab in labels)
            {
                TodoItemLabel existingLab = _repository.GetLabel(lab);
                if (existingLab != null)
                {
                    //existingLab.LabelTodoItems.Add(item);
                    item.Labels.Add(existingLab);
                }
                else
                {
                    TodoItemLabel newLabel = new TodoItemLabel(lab);
                    newLabel.LabelTodoItems.Add(item);
                    item.Labels.Add(newLabel);
                }
            }
            _repository.Add(item);

            return(RedirectToAction("Index"));
        }