コード例 #1
0
        public async Task <IActionResult> AddItem(AddTodoViewModel added)
        {
            if (ModelState.IsValid)
            {
                var user = await FetchUser();

                var item = new TodoItem(added.Text, new Guid(user.Id))
                {
                    DateCreated = DateTime.Now,
                    DateDue     = added.DateDue
                };

                _repository.Add(item);

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

                    foreach (string s in labels)
                    {
                        _repository.AddLabel(s.Trim().ToLower(), item.Id);
                    }
                }

                return(RedirectToAction("Index"));
            }

            return(View(added));
        }
コード例 #2
0
        public async Task <IActionResult> AddItem(AddTodoViewModel addTodoViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(addTodoViewModel));
            }
            ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);

            TodoItem item = new TodoItem(addTodoViewModel.TodoText, new Guid(user.Id));

            item.DateCreated = DateTime.Now;
            item.DateDue     = addTodoViewModel.DateDue;
            _repository.Add(item);

            if (addTodoViewModel.Labels != null)
            {
                string[] labels = addTodoViewModel.Labels.Split(',');
                foreach (string label in labels)
                {
                    _repository.AddLabel(label.Trim().ToLower(), item.Id);
                }
            }

            return(RedirectToAction("Index"));
        }
コード例 #3
0
ファイル: TodoController.cs プロジェクト: ivonamr/raupjc-hw3
        public async Task <IActionResult> Add(AddTodoViewModel it)

        {
            if (ModelState.IsValid)

            {
                ApplicationUser applicationUser = await _userManager.GetUserAsync(HttpContext.User);

                TodoItem todo = new TodoItem(it.Text, new Guid(applicationUser.Id))

                {
                    DateDue = it.DateDue
                };

                if (it.Labels != null)

                {
                    string[] labels = it.Labels.Split(',');

                    List <TodoItemLabel> todoItemLabels = new List <TodoItemLabel>();

                    foreach (String label in labels)

                    {
                        _repository.AddLabel(label.Trim(), todo.Id, todo.UserId);
                    }
                }

                _repository.Add(todo);

                return(RedirectToAction("Index"));
            }

            return(View());
        }
コード例 #4
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());
        }
コード例 #5
0
        public async Task <IActionResult> Add(AddTodoViewModel model)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser currentUser = await _userManager.GetUserAsync(HttpContext.User);

                TodoItem item;

                try
                {
                    item = new TodoItem(model.Text, Guid.Parse(await _userManager.GetUserIdAsync(currentUser)));
                }
                catch (FormatException ex)
                {
                    _repository.AddError(ex.Message);
                    return(View("Error"));
                }
                catch (ArgumentNullException ex)
                {
                    _repository.AddError(ex.Message);
                    return(View("Error"));
                }
                try
                {
                    item.DateDue = model.DateDue;
                    if (model.Label != null)
                    {
                        string[] labels = model.Label.Split(',');
                        foreach (string l in labels)
                        {
                            if (l.Trim() != "")
                            {
                                _repository.AddLabel(item, l.Trim());
                            }
                        }
                    }

                    _repository.Add(item);
                    return(RedirectToAction("Index"));
                }
                catch (DuplicateTodoItemException ex)
                {
                    _repository.AddError(ex.Message);
                    return(View());
                }
            }
            return(View());
        }
コード例 #6
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"));
        }
コード例 #7
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"));
        }
コード例 #8
0
ファイル: TodoController.cs プロジェクト: warzy96/raupjc-hw3
        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));
        }