예제 #1
0
        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));
        }
        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
        }
        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
        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));
            }
        }
        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));
        }
        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);
        }
        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));
 }