コード例 #1
0
ファイル: TodosController.cs プロジェクト: smack0007/DDC2020
        public ActionResult Add([FromBody] TodoCreateDto dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            var entity = _mapper.Map <TodoEntity>(dto);

            entity.Created = DateTime.Now;

            var newEntity = _todoRespository.Add(entity);

            bool success = _todoRespository.Save();

            if (!success)
            {
                throw new Exception("Adding an item failed on save.");
            }

            return(CreatedAtRoute(
                       nameof(GetById),
                       new { id = newEntity.Id },
                       _mapper.Map <TodoDto>(entity)));
        }
コード例 #2
0
        public new TodoDto Insert(TodoCreateDto item)
        {
            var inserted = base.Insert(item);

            todoCache.SourceCache.AddOrUpdate(inserted);
            return(inserted);
        }
コード例 #3
0
        public ActionResult AddTodo([FromBody] TodoCreateDto todoCreateDto)
        {
            if (todoCreateDto == null)
            {
                return(BadRequest());
            }

            TodoEntity item = _mapper.Map <TodoEntity>(todoCreateDto);

            item.Created = DateTime.UtcNow;
            TodoEntity newTodoEntity = _todoRepository.Add(item);

            if (!_todoRepository.Save())
            {
                throw new Exception("Adding an item failed on save.");
            }

            var dto = _mapper.Map <TodoDto>(newTodoEntity);

            _todoHubContext.Clients.All.SendAsync("TodoAdded", dto);

            return(CreatedAtRoute(
                       nameof(GetSingle),
                       new { id = newTodoEntity.Id },
                       dto));
        }
コード例 #4
0
        public ActionResult <TodoReadDto> Create(TodoCreateDto dto)
        {
            int userId = Convert.ToInt32(HttpContext.Items["id"]);

            TodoReadDto result = _service.Create(userId, dto);

            return(Ok(result));
        }
コード例 #5
0
        public ActionResult <TodoReadDto> CreateTodoItem(TodoCreateDto todoCreateDto)
        {
            var todo = _mapper.Map <TodoItem>(todoCreateDto);

            _repository.CreateTodoItem(todo);
            _repository.SaveChanges();

            var todoReadDto = _mapper.Map <TodoReadDto>(todo);

            return(CreatedAtAction(nameof(GetTodoItemById), new { id = todoReadDto.Id }, todoReadDto));
        }
コード例 #6
0
        public ActionResult <Todo> CreateNewTodo(TodoCreateDto todoCreateDto)
        {
            var newTodo = _mapper.Map <Todo>(todoCreateDto);

            _repository.CreateNewTodo(newTodo);
            _repository.SaveChanges();

            var responseBody = _mapper.Map <TodoReadDto>(newTodo);

            return(CreatedAtRoute(nameof(GetTodoById), new { Id = newTodo.Id }, responseBody));
        }
コード例 #7
0
        public TodoReadDto Create(int userId, TodoCreateDto dto)
        {
            Todo entity = dto.ToEntity();

            entity.UserId = userId;

            _repository.Create(entity);
            _repository.SaveChanges();

            return(entity.ToReadDto());
        }
コード例 #8
0
        public async Task <Todo> Create(TodoCreateDto data)
        {
            var result = await _dbContext.Todos.AddAsync(new Todo
            {
                Name      = data.Name,
                Completed = data.Completed
            });

            await _dbContext.SaveChangesAsync();

            return(result.Entity);
        }
コード例 #9
0
        public ActionResult <TodoReadDto> CreateTodo(TodoCreateDto todoCreateDto)
        {
            var todoModel = _mapper.Map <Todo>(todoCreateDto);

            _repository.CreateTodo(todoModel);
            _repository.SaveChanges(); // persist

            var todoReadDto = _mapper.Map <TodoReadDto>(todoModel);

            //return Ok(todoReadDto); // raw response

            return(CreatedAtRoute(nameof(GetTodo), new { Id = todoReadDto.Id }, todoReadDto)); // return location
        }
コード例 #10
0
        public TodoDto Insert(TodoCreateDto item)
        {
            var now = DateTime.Now;
            var id  = db.ExecuteScalar <int>(SqlQueries.InsertTodo, new
            {
                Description = item.Description,
                Completed   = item.Completed,
                Updated     = now,
                Created     = now
            });

            return(Get(id));
        }
コード例 #11
0
ファイル: TodoController.cs プロジェクト: HRKings/TodoAPI
        public async Task <IActionResult> Create([FromServices] ITodoRepository repository,
                                                 [FromBody] TodoCreateDto todo)
        {
            try
            {
                var result = await repository.Create(todo);

                return(Ok(result));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
コード例 #12
0
        public TodoDto Insert(TodoCreateDto item)
        {
            var clone = new TodoDto
            {
                Id          = idCounter++,
                Description = item.Description,
                Completed   = item.Completed,
                Created     = DateTimeOffset.Now,
                Updated     = DateTimeOffset.Now
            };

            items.Add(clone);
            return(clone);
        }
コード例 #13
0
        public async Task <bool> CreateTodo(TodoCreateDto todo, string email)
        {
            Todo newTodo = new Todo()
            {
                Header      = todo.Header,
                Priority    = todo.Priority,
                CreatedTime = DateTime.Now,
                AuthorEmail = email,
            };

            await _context.Todos.AddAsync(newTodo);

            int result = await _context.SaveChangesAsync();

            return(result > 0);
        }
コード例 #14
0
        public IActionResult CreateTodo([FromBody] TodoCreateDto todoCreateDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var todoEntity = _mapper.Map <TodoEntity>(todoCreateDto);

            _todoRepository.CreateTodo(todoEntity);
            _todoRepository.Save();

            var todoToReturn = _mapper.Map <TodoDto>(todoEntity);

            return(CreatedAtRoute("GetTodo", new { id = todoToReturn.Id }, todoToReturn));
        }
コード例 #15
0
        public async Task <IActionResult> Add([FromBody] TodoCreateDto newTodo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string email       = Helper.GetEmail(HttpContext);
            var    isSuccesful = await _repo.CreateTodo(newTodo, email);

            if (isSuccesful)
            {
                return(Ok("Todo added succesful"));
            }

            return(StatusCode(500));
        }
コード例 #16
0
        public IActionResult AddTodo([FromBody] TodoCreateDto viewModel)
        {
            if (viewModel == null)
            {
                return(BadRequest());
            }

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

            TodoItem addItem = Mapper.Map <TodoItem>(viewModel);

            _toDoRepository.Insert(addItem);

            TodoItem newItem = _toDoRepository.GetSingle(addItem.Id);

            return(CreatedAtRoute("GetTodoById", new { id = newItem.Id }, Mapper.Map <TodoDto>(newItem)));
        }
コード例 #17
0
        public async Task <Todo> Create(TodoCreateDto data)
        {
            await using var connection =
                            new NpgsqlConnection(Environment.GetEnvironmentVariable("DATABASE"));

            await connection.OpenAsync();

            var resultingId =
                await connection.ExecuteScalarAsync <int>("INSERT INTO todos(name, completed) VALUES (@name, @completed) RETURNING id",
                                                          new
            {
                name      = data.Name,
                completed = data.Completed
            });

            var result = await connection.QueryFirstAsync <Todo>("SELECT * FROM todos WHERE id = @ID", new { ID = resultingId });

            await connection.CloseAsync();

            return(result);
        }
コード例 #18
0
        public async Task <ActionResult> AddTodo(ApiVersion version, [FromBody] TodoCreateDto todoCreateDto)
        {
            if (todoCreateDto == null)
            {
                return(BadRequest());
            }

            TodoEntity item = _mapper.Map <TodoEntity>(todoCreateDto);

            item.Created = DateTime.UtcNow;
            TodoEntity newTodoEntity = _todoRepository.Add(item);

            if (!_todoRepository.Save())
            {
                throw new Exception("Adding an item failed on save.");
            }

            await _todoHubContext.Clients.All.SendAsync("todo-added", newTodoEntity);

            return(CreatedAtRoute(
                       nameof(GetSingle),
                       new { version = version.ToString(), id = newTodoEntity.Id },
                       _mapper.Map <TodoDto>(newTodoEntity)));
        }
コード例 #19
0
 public static Todo ToEntity(this TodoCreateDto dto) => new Todo
 {
     Checked = dto.Checked,
     Text    = dto.Text,
 };
コード例 #20
0
 public TodoDto InsertSingle(
     [FromBody] TodoCreateDto newItemDto)
 {
     return(repository.Insert(newItemDto));
 }