public async Task <IActionResult> AddItem(TodoItemViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }

            var newItem = new TodoItem()
            {
                Id       = model.Id,
                Title    = model.Title,
                DueAt    = model.DueAt,
                Category = await _categoryService.GetByIdAsync(model.CategoryId)
            };

            var successful = await _todoItemService.AddItemAsync(newItem, currentUser);

            if (!successful)
            {
                return(BadRequest("Could not add item."));
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <TodoItem> > CreateItem([FromBody] TodoItemDto item)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                _logger.LogError($"Unknown user tried creating an item.");
                return(Unauthorized());
            }

            if (item == null)
            {
                _logger.LogError($"Received null item.");
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError($"Received invalid item.");
                return(BadRequest());
            }
            if (item.Done == null)
            {
                item.Done = false;
            }

            // create mapping
            var dbItem = _mapper.Map <TodoItem>(item);
            await _todoService.AddItemAsync(dbItem, user);

            _logger.LogInformation($"Created new TodoItem with id {dbItem.Id}");
            return(CreatedAtAction(nameof(GetItemById), new { Id = dbItem.Id }, dbItem));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            // ModelState.IsValid refers to model validation of TodoItem passed from the form
            // It is customary to do this check right at the beginning of the action.
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }

            var successful = await _todoItemService
                             .AddItemAsync(newItem, currentUser);

            if (!successful)
            {
                return(BadRequest("Could not add item."));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            // if missing Title (required) attribute will be invalid
            if (!ModelState.IsValid)
            {
                Console.WriteLine("Invalid input, missing Title");
                // Return to the current page
                return(RedirectToAction("Index"));
            }

            //var successful = await _todoItemService.AddItemAsync(newItem);

            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }
            //var successful = true;
            var successful = await _todoItemService.AddItemAsync(newItem, currentUser);


            if (!successful)
            {
                return(BadRequest("Could not add item."));
            }

            // Return to the current page
            return(RedirectToAction("Index"));
        }
Exemplo n.º 5
0
        /*用于防止跨站请求伪造,即保证我的链接跳转到的是我的程序*/
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                /*ModelState为"模型核验"的结果,core会通过开发者设定的规则对输入字符串进行检验
                 * TodoItem上Title字段上方的[Required]即代表一个检验标准,相当于"not null"
                 * 习惯上,先核验再操作*/
                //valid:非法
                return(RedirectToAction("Index"));//如果核验不通过则重定向回去
            }

            var currentUser = await _userManager.GetUserAsync(User);

            var successful = await _todoItemService.AddItemAsync(newItem, currentUser);

            /*就是获取用户名之后将其加入方法之中*/

            if (!successful)
            {
                return(BadRequest("can't add it"));
                //跳转到400 error page
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            //Obtenemos el Usario actual
            var currentUser = await _userManager.GetUserAsync(User);

            //Si falta algo de información, forzamos a que el usario inicie sesión
            if (currentUser == null)
            {
                return(Challenge());
            }

            var successful = await _todoItemService
                             .AddItemAsync(newItem, currentUser);

            if (!successful)
            {
                return(BadRequest("Could not add item."));
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Create([Bind("Title,Content,DuetoDateTime,Tags")] TodoItemCreateViewModel todo)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }

            var todoItem = new TodoItem
            {
                Title         = todo.Title,
                Content       = todo.Content,
                DuetoDateTime = todo.DuetoDateTime,
                Tags          = todo.Tags != null?todo.Tags.Split(',') : new[] { "" }
            };
            var successful = await _todoItemService
                             .AddItemAsync(todoItem, currentUser);

            if (!successful)
            {
                return(BadRequest(new { error = "Could not add item." }));
            }


            return(RedirectToAction("Index"));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }


            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }


            var successful = await _todoItemService.AddItemAsync(newItem, currentUser);

            if (!successful)
            {
                return(BadRequest("Could not add item."));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 9
0
        // this method will carry a parameter (a TodoItem object with a Title property) returned from a submit form from a partial view, AddItemPartial.cshtml
        // this method will also perform a 'model binding'
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }

            // the controller calls into the service layer to save the new to-do item.
            // AddItemAsync method will return true of false value.
            var successful = await _todoItemService.AddItemAsync(newItem, currentUser);

            if (!successful)
            {
                return(BadRequest(new { error = "Could not add item." }));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 10
0
      public async Task <IActionResult> AddAnswer(QOTDAnswer newAnswer, IFormCollection form)
      {
          if (!ModelState.IsValid)
          {
              return(RedirectToAction("About"));
          }

          int intIdt = _context.QOTDQuestion.Max(u => u.id);

          newAnswer.QuestionID = intIdt + 1;

          List <string> AuthorIds = new List <string> {
          };

          foreach (string item in form["Authors"])
          {
              AuthorIds.Add(item);
          }
          var successful = false;

          if (AuthorIds.Count() <= 1)
          {
              newAnswer.Author = AuthorIds[0];
              successful       = await _todoItemService.AddItemAsync(newAnswer);
          }
          else
          {
              List <QOTDAnswer> allAuthors = new List <QOTDAnswer> ()
              {
              };
              foreach (string author in AuthorIds)
              {
                  newAnswer.Author = author;
                  allAuthors.Add(newAnswer);
                  successful = await _todoItemService.AddItemsAsync(allAuthors);
              }
          }
          if (!successful)
          {
              return(BadRequest("Could not add item."));
          }

          return(RedirectToAction("About", new{ QuestionID = intIdt + 1 }));
      }
Exemplo n.º 11
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            await _todoItemService.AddItemAsync(newItem);

            return(RedirectToAction("Index"));
        }
Exemplo n.º 12
0
        public async Task <IActionResult> OnPostAddItemAsync(TodoItem item)
        {
            IdentityUser user;

            user = await this.GetCurrentUser();

            await _todoItemService.AddItemAsync(item, user);

            return(this.Redirect("./"));
        }
Exemplo n.º 13
0
        public async Task <ActionResult <TodoItemCreateDTO> > CreateItem(TodoItemCreateDTO dtoItem)
        {
            var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

            dtoItem.UserId = userId;
            var result = mapper.Map <ToDoItem>(dtoItem);
            await toDoService.AddItemAsync(result);

            var todoItemCreate = mapper.Map <TodoItemReadDTO>(result);

            return(Ok(todoItemCreate));
        }
Exemplo n.º 14
0
        public async Task <IActionResult> AddItem(TodoItem item)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }
            var result = await _todoItemService.AddItemAsync(item);

            if (!result)
            {
                return(BadRequest("Faild"));
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 15
0
        public async Task <IActionResult> AddItem(NewTodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var successful = await _todoItemService.AddItemAsync(newItem);

            if (!successful)
            {
                return(BadRequest(new { error = "Could not add item" }));
            }
            return(Ok());
        }
Exemplo n.º 16
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }
            var successful = await _todoItemService.AddItemAsync(newItem);

            if (!successful)
            {
                return(BadRequest("Could not add item."));
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 17
0
        public async Task<IActionResult> AddItem(TodoItemModel newItem)
        {
            if (!ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }

            var succesful = await todoItemService.AddItemAsync(newItem);
            if (!succesful)
            {
                return BadRequest(new { error = "Could not add item" });
            }

            return RedirectToAction("Index");
        }
Exemplo n.º 18
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await _service.AddItemAsync(newItem);

            if (!result)
            {
                return(BadRequest(new { error = "Could not add item" }));
            }

            return(Ok());
        }
Exemplo n.º 19
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            var successful = await _todoItemService.AddItemAsync(newItem);

            if (!successful)
            {
                return(BadRequest("Não é possível adicionar um item"));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 20
0
        public async Task <IActionResult> AddItem(TodoItem item)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            var exito = await _todoItemService.AddItemAsync(item);

            if (!exito)
            {
                return(BadRequest("No se pudo agregar el item"));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 21
0
        public async Task <IActionResult> AddItem(NewTodoItem newItem)
        {
            if (!ModelState.IsValid) //if the model returns an error
            {
                //do stuff
                return(BadRequest(ModelState));
            }
            //check if the adding is successful (we still need to add AddItemAsync as it throws an error)
            bool IsSuccessful = await _todoItemService.AddItemAsync(newItem);

            if (!IsSuccessful) //if it fails
            {
                return(BadRequest(new { error = "Could not add item" }));
            }
            //otherwise, if it works return OK result
            return(Ok());
        }
Exemplo n.º 22
0
        public async Task <IActionResult> AddItem(TodoItem item)
        {
            bool x = TryValidateModel(item);

            if (!ModelState.IsValid) //if(TryValidateModel(item))
            {
                return(BadRequest(ModelState));
            }

            var result = await _service.AddItemAsync(item); //TODO: Geri dönen bilgiye göre yeşil renkte onay mesajı gösterilmesi.

            if (!result)
            {
                return(BadRequest(new { error = "Eklenemedi." }));
            }

            return(Ok());
        }
Exemplo n.º 23
0
        public async Task <IActionResult> AddItem(AddTodoItemModel newItemModel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            var user = await _userManager.GetUserAsync(User);

            var successful = await _todoItemService.AddItemAsync(newItemModel, user);

            if (!successful)
            {
                return(BadRequest("Could not add item."));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 24
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(View("AddItemPartial", newItem));
            }

            var currentUser = await _userManager.GetUserAsync(User);

            var successful = await _todoItemService.AddItemAsync(newItem, currentUser);

            if (!successful)
            {
                ViewData["Error"] = $"Could not add Todo Item {newItem.Title}";
                return(View("AddItemPartial", newItem));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 25
0
        public async Task <IActionResult> AddItem(TodoItem item)
        {
            bool x = TryValidateModel(item);

            TempData["Success"] = "Added Successfully!";

            if (!ModelState.IsValid) //if(TryValidateModel(item))
            {
                return(BadRequest(ModelState));
            }

            var result = await _service.AddItemAsync(item); //TODO: Geri dönen bilgiye göre yeşil renkte onay mesajı gösterilmesi.(TAMAMLANDI)

            if (!result)
            {
                return(BadRequest(new { error = "Eklenemedi." }));
            }
            return(Ok(new { success = "Eklendi." }));
        }
Exemplo n.º 26
0
        public async Task <IActionResult> AddItem(NewTodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Unauthorized());
            }
            var successful = await _todoItemService.AddItemAsync(newItem, currentUser);

            if (!successful)
            {
                return(BadRequest(new { error = "Could not add item" }));
            }
            return(Ok());
        }
Exemplo n.º 27
0
        public async Task <IActionResult> AddItem(NewTodoItemViewModel newItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            ApplicationUser currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }
            bool sucessful = await _todoItemService.AddItemAsync(newItem, currentUser);

            if (!sucessful)
            {
                return(BadRequest(new { error = "Could not add item" }));
            }
            return(Ok());
        }
Exemplo n.º 28
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }
            // Obtener usuario.
            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }
            var successful = await _todoItemService.AddItemAsync(newItem, currentUser);

            if (!successful)
            {
                return(BadRequest("No fue posible agregar la tarea."));
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 29
0
        public async Task <IActionResult> AddItem(TodoItem newItem)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }
            //Obtener usuario actual, si no existe devuelve un error
            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }
            //Agrega 1 nuevo item a la base de datos, nos avisa si se pudo o no guardar;
            var successful = await _todoItemService.AddItemAsync(newItem, currentUser);

            if (!successful)
            {
                return(BadRequest("Could not add item."));
            }
            return(RedirectToAction("Index"));
        }
        public TodoItemServiceShould()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_AddNewItem")
                          .Options;

            _context  = new ApplicationDbContext(options);
            _service  = new TodoItemService(_context);
            _fakeUser = new ApplicationUser
            {
                Id       = "fake-000",
                UserName = "******",
            };
            _service.AddItemAsync(
                new TodoItem
            {
                Title = "Testing?",
                DueAt = DateTimeOffset.Now.AddDays(3)
            },
                _fakeUser
                );
        }