示例#1
0
        public async Task <IActionResult> SaveTodoEntity([FromBody] TodoVM pTodo)
        {
            try
            {
                if (pTodo.Id.HasValue && pTodo.Id.Value > 0)
                {
                    // The user application exists
                    TodoEntity tde = _repository.GetTodoList().Where(ua => ua.Id == pTodo.Id).FirstOrDefault <TodoEntity>();
                    setTodoEntity(pTodo, tde);

                    bool isSaved = await _repository.SaveChangesAsync();

                    // return OK
                    return(Ok(new { Data = pTodo, Status = "Success" }));
                }
                else
                {
                    //the user application doesn't exists
                    TodoEntity tde = new TodoEntity();
                    setTodoEntity(pTodo, tde);
                    tde = await _repository.InsertTodoEntity(tde);

                    pTodo.Id = tde.Id;

                    // return OK
                    return(Ok(new { Data = pTodo, Status = "Success" }));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"{ex.Message}");
                return(BadRequest(new { Status = "Error", Error = $"{ex.Message}" }));
            }
        }
示例#2
0
        public async Task <ActionResult <TodoItem> > CreateTodoItem([FromBody] TodoItemForCreationDto todoItemForCreation)
        {
            var todoItemEntity = _mapper.Map <TodoItem>(todoItemForCreation);

            _repo.AddTodoItem(todoItemEntity);

            await _repo.SaveChangesAsync();

            var todoItemDto = _mapper.Map <TodoItemDto>(todoItemEntity);

            return(CreatedAtRoute("GetTodoItem", new { todoItemId = todoItemEntity.Id }, todoItemDto));
        }