Beispiel #1
0
        public async Task <IActionResult> Add(AddTodoViewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            if (ModelState.IsValid)
            {
                var todoItem = new TodoItem(model.Text, new Guid(user.Id))
                {
                    DateDue = model.DateDue
                };
                await _repository.Add(todoItem);

                if (model.Labels != null)
                {
                    string[] split = model.Labels.Split(",".ToCharArray());
                    foreach (var label in split)
                    {
                        var lab = await _repository.AddLabelToDb(new TodoItemLabel(label.ToUpper().Trim()));

                        await _repository.AddLabelToTodoItem(lab, todoItem);
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
Beispiel #2
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());
        }