public async Task <ActionResult <TodoItem> > PostTodoItem(TodoItem item) { _context.Todos.Add(item); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetTodoItem), new { id = item.Id }, item)); }
public async Task <IActionResult> PutTodo([FromRoute] int id, [FromBody] Todo todo) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } todo.id = id; _context.Entry(todo).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutTodoItem(long id, TodoItem todoItem) { if (id != todoItem.Id) { return(BadRequest()); } _context.Entry(todoItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutTodoItems([FromBody] TodoItem[] todos) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } foreach (TodoItem item in todos) { _context.Entry(item).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoItemExists(item.id)) { return(NotFound()); } else { throw; } } } return(NoContent()); }
public async Task <IActionResult> Post([FromBody] Todo todo) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //Add todo to Todos await _context.TodoItems.AddAsync(todo); await _context.SaveChangesAsync(); //Send back 201 status "created" return(CreatedAtRoute("GetByID", new { id = todo.ID }, todo)); }
public async Task <bool> AddTodoItem_Details(TodoItem_Details TodoItem_Details) { try { await _context.TodoItem_Details.AddAsync(TodoItem_Details); await _context.SaveChangesAsync(); return(true); } catch (Exception ex) { throw new TODOCustomError($"Erorr While adding Todod item : {ex.Message}"); } }
public async Task <Todo> Create(Todo todo) { if (todo == null) { throw new Exception("Todo cannot be null"); } if (todo.Id != 0) { throw new Exception("Tode.Id must be zero"); } _context.Todos.Add(todo); var rowsAffected = await _context.SaveChangesAsync(); if (rowsAffected != 1) { throw new Exception("Create failed"); } return(todo); }
public async Task <T> Add(T entity) { if (entity == null) { throw new ArgumentNullException($"{nameof(Add)} entity must not be null"); } try { await _context.AddAsync(entity); await _context.SaveChangesAsync(); return(entity); } catch (Exception ex) { throw new Exception($"{nameof(entity)} could not be saved: {ex.Message}"); } }
public async Task <IActionResult> PutTodoEntry(int id, TodoEntry todoEntry) { todoEntry.id = id; _context.Entry(todoEntry).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoEntryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public override async Task <AddTodoReply> AddTodo(AddTodoRequest request, ServerCallContext context) { var reply = new AddTodoReply() { Text = request.Text + " added" }; var todo = new Domain.Entities.Todo(); todo.Text = request.Text; dbContext.Todo.Add(todo); await dbContext.SaveChangesAsync(); await _todoHub.Clients.All.SendAsync("ReceiveTodo", new TodoItem() { Id = todo.Id.ToString(), Text = todo.Text }); return(reply); }