Пример #1
0
        public async Task <IActionResult> Add(AddTodoViewModel model)
        {
            ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);

            Guid userId = new Guid(user.Id);

            TodoItem newItem = new TodoItem(model.Text, userId);

            if (model.DateDue != null)
            {
                newItem.DateDue = (DateTime)model.DateDue;
            }
            else
            {
                newItem.DateDue = null;
            }
            if (!string.IsNullOrWhiteSpace(model.Labels))
            {
                string[] labelValues = model.Labels.Split(',');
                foreach (var labelValue in labelValues)
                {
                    string trimmedLabelValue = labelValue.Trim();

                    //if it isnt empty or null
                    if (!string.IsNullOrWhiteSpace(trimmedLabelValue))
                    {
                        TodoLabel existingLabel = _repository.GetLabel(trimmedLabelValue);
                        if (existingLabel == null)
                        {
                            TodoLabel newLabel = new TodoLabel(trimmedLabelValue);
                            _repository.AddLabel(newLabel);
                            newItem.Labels.Add(newLabel);
                        }
                        else
                        {
                            newItem.Labels.Add(existingLabel);
                        }
                    }
                }
            }

            _repository.Add(newItem);
            return(RedirectToAction("Index"));
        }
Пример #2
0
        public ActionResult Add(AddTodoViewModel todoViewModel)
        {
            Guid userId = getUserId();

            if (!ModelState.IsValid)
            {
                return(View(todoViewModel));
            }

            TodoItem item = new TodoItem();

            if (todoViewModel.Labels != null)
            {
                string[]         splits    = todoViewModel.Labels.Split(',');
                List <TodoLabel> labelList = new List <TodoLabel>();

                foreach (string label in splits)
                {
                    TodoLabel todoLabel = new TodoLabel(label.Trim());

                    labelList.Add(todoLabel);
                }

                item.Labels = labelList;
            }

            if (todoViewModel.DateDue.Equals(DateTime.MinValue))
            {
                item.DateDue = new Nullable <DateTime>();
            }
            else
            {
                item.DateDue = todoViewModel.DateDue;
            }
            item.Text          = todoViewModel.Text;
            item.UserId        = userId;
            item.DateCompleted = new Nullable <DateTime>();



            _repository.Add(item);

            return(RedirectToAction("Index"));
        }
Пример #3
0
        public async Task <IActionResult> Edit(Guid id, [Bind("Title,Description")] Todo todo, List <Guid> TodoLabels)
        {
            if (ModelState.IsValid)
            {
                // If the Todo doesn't belong to our user
                Todo curTodo = _context.Todo.First(t => t.Id == id && t.User == GetCurrentUser().Result);
                if (curTodo == null)
                {
                    return(Unauthorized());
                }
                curTodo.Description          = todo.Description;
                curTodo.Title                = todo.Title;
                curTodo.LastModificationDate = DateTime.Now;
                curTodo.TodoLabels           = new List <TodoLabel>();
                foreach (Guid labelId in TodoLabels)
                {
                    TodoLabel tdl = new TodoLabel();
                    tdl.Todo      = curTodo;
                    tdl.LabelGuid = labelId;
                    curTodo.TodoLabels.Add(tdl);
                }
                try{
                    _context.Update(curTodo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException) {
                    if (!TodoExists(todo.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            TodoViewModel tvm = new TodoViewModel();

            tvm.Todo   = todo;
            tvm.Labels = _context.Label.ToList();
            return(View(tvm));
        }
Пример #4
0
        public async Task <IActionResult> Create([Bind("Title,Description")] Todo todo, List <Guid> TodoLabels)
        {
            if (ModelState.IsValid)
            {
                todo.Id                   = Guid.NewGuid();
                todo.User                 = GetCurrentUser().Result;
                todo.CreationDate         = DateTime.Now;
                todo.LastModificationDate = DateTime.Now;
                todo.Done                 = false;
                todo.TodoLabels           = new List <TodoLabel>();
                foreach (Guid labelId in TodoLabels)
                {
                    TodoLabel tdl = new TodoLabel();
                    tdl.Todo      = todo;
                    tdl.LabelGuid = labelId;
                    todo.TodoLabels.Add(tdl);
                }
                _context.Add(todo);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(todo));
        }