示例#1
0
        public async Task <IActionResult> Create([Bind("Title,Color")] ToDoItemCreateModel toDoItem)
        {
            if (ModelState.IsValid)
            {
                await _toDo.AddAsync(new Item <Guid, string>
                {
                    Color = toDoItem.Color,
                    Title = toDoItem.Title
                });

                return(RedirectToAction(nameof(Index)));
            }
            return(View(toDoItem));
        }
示例#2
0
        public async Task <ActionResult <ToDoItemViewModel> > Post(ToDoItemCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var toDoItemObj = _mapper.Map <ToDoItem>(model);

            _context.ToDoItems.Add(toDoItemObj);
            await _context.SaveChangesAsync();

            var createdToDoItem = await _context.ToDoItems.FindAsync(toDoItemObj.Id);

            var mappedItem = _mapper.Map <ToDoItemViewModel>(createdToDoItem);

            return(Ok(mappedItem));
        }
示例#3
0
        public async Task <IActionResult> CreateItem(ToDoItemCreateModel createModel)
        {
            _logger.LogInformation("CreateItem() calles");
            if (string.IsNullOrWhiteSpace(createModel.Title) || string.IsNullOrWhiteSpace(createModel.Description))
            {
                return(new BadRequestObjectResult("tile and description must contain content"));
            }

            var newItem = new ToDoItem
            {
                Id          = Guid.NewGuid(),
                Title       = createModel.Title,
                Description = createModel.Description
            };
            await _database.Add(newItem);

            var readModel = ToReadModel(newItem);

            return(Created(nameof(readModel), readModel));
        }