コード例 #1
0
        public async Task <ActionResult <ToDoItemCreateDto> > PostToDoItem(ToDoItemCreateDto toDoItemCreateDto)
        {
            var user = _context.Users
                       .Include(c => c.Categories)
                       .FirstOrDefault(x => x.UserName.ToLower() == toDoItemCreateDto.UserName.ToLower());

            var cat = user.Categories
                      .FirstOrDefault(x => x.Name == toDoItemCreateDto.CategoryName);

            var item = new ToDoItem
            {
                AppUser     = user,
                CategoryId  = cat.Id,
                AppUserId   = user.Id,
                Description = toDoItemCreateDto.Description,
                Category    = cat
            };

            //TEST
            _context.ToDoItems.Add(item);

            await AddToSpecifiedUserToDoItem(user.Id, item);
            await AddToSpecifiedCategory(cat.Id, item);

            await _context.SaveChangesAsync();

            return(new ToDoItemCreateDto
            {
                Description = toDoItemCreateDto.Description,
                UserName = toDoItemCreateDto.UserName,
                CategoryName = toDoItemCreateDto.CategoryName
            });
        }
コード例 #2
0
        public IActionResult Post([FromBody] ToDoItemCreateDto toDoItem)
        {
            try
            {
                string userIdFromUserManager = User.Claims.First(c => c.Type == "UserId").Value;

                var toDoItemToCreate = new ToDoItem
                {
                    Id     = ObjectId.GenerateNewId(),
                    Text   = toDoItem.Text,
                    Title  = toDoItem.Title,
                    IsDone = toDoItem.IsDone,
                    UserId = userIdFromUserManager
                };

                _toDoService.AddToDoItem(toDoItemToCreate);

                return(CreatedAtRoute(new
                {
                    id = toDoItemToCreate.Id
                },
                                      toDoItemToCreate));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
        }
コード例 #3
0
        public async Task <ActionResult <bool> > Put(string id, [FromBody] ToDoItemCreateDto toDo)
        {
            try
            {
                ObjectId toDoItemId = new ObjectId(id);

                var toDoItemFromRepo = await _toDoService.GetToDoItem(toDoItemId);

                var toDoToPut = new ToDoItem
                {
                    Id     = toDoItemFromRepo.Id,
                    Text   = toDo.Text,
                    Title  = toDo.Title,
                    IsDone = toDo.IsDone,
                    UserId = toDoItemFromRepo.UserId
                };

                return(await _toDoService.UpdateToDoItem(toDoItemId, toDoToPut));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
        }