// POST api/Todo public HttpResponseMessage PostTodoItem(TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } TodoList todoList = db.TodoLists.Find(todoItemDto.TodoListId); if (todoList == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } if (todoList.UserId != User.Identity.Name) { // Trying to add a record that does not belong to the user return Request.CreateResponse(HttpStatusCode.Unauthorized); } TodoItem todoItem = todoItemDto.ToEntity(); // Need to detach to avoid loop reference exception during JSON serialization db.Entry(todoList).State = EntityState.Detached; db.TodoItems.Add(todoItem); db.SaveChanges(); todoItemDto.TodoItemId = todoItem.TodoItemId; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todoItemDto); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = todoItemDto.TodoItemId })); return response; }
// POST api/Todo public HttpResponseMessage PostTodoItem(TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } TodoList todoList = db.TodoLists.Find(todoItemDto.TodoListId); if (todoList == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (todoList.UserId != User.Identity.Name) { // Trying to add a record that does not belong to the user return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } TodoItem todoItem = todoItemDto.ToEntity(); // Need to detach to avoid loop reference exception during JSON serialization db.Entry(todoList).State = EntityState.Detached; db.TodoItems.Add(todoItem); db.SaveChanges(); todoItemDto.TodoItemId = todoItem.TodoItemId; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todoItemDto); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = todoItemDto.TodoItemId })); return(response); }
// POST api/Todo public HttpResponseMessage PostTodoItem(TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } TodoList todoList = db.TodoLists.Find(todoItemDto.TodoListId); if (todoList == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (todoList.UserId != User.Identity.Name) { // 正在尝试添加不属于用户的记录 return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } TodoItem todoItem = todoItemDto.ToEntity(); // 需要分离,以避免在 JSON 序列化期间发生循环引用异常 db.Entry(todoList).State = EntityState.Detached; db.TodoItems.Add(todoItem); db.SaveChanges(); todoItemDto.TodoItemId = todoItem.TodoItemId; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todoItemDto); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = todoItemDto.TodoItemId })); return(response); }
// POST api/Todo public HttpResponseMessage PostTodoItem(TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } TodoList todoList = db.TodoLists.Find(todoItemDto.TodoListId); if (todoList == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } if (todoList.UserId != User.Identity.Name) { // 사용자에게 속하지 않은 레코드를 추가하려고 시도합니다. return Request.CreateResponse(HttpStatusCode.Unauthorized); } TodoItem todoItem = todoItemDto.ToEntity(); // JSON 직렬화가 이루어질 때 루프 참조 예외 발생을 방지하려면 분리해야 합니다. db.Entry(todoList).State = EntityState.Detached; db.TodoItems.Add(todoItem); db.SaveChanges(); todoItemDto.TodoItemId = todoItem.TodoItemId; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todoItemDto); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = todoItemDto.TodoItemId })); return response; }
// POST api/Todo public HttpResponseMessage PostTodoItem(TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } TodoList todoList = db.TodoLists.Find(todoItemDto.TodoListId); if (todoList == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (todoList.UserId != User.Identity.Name) { // ユーザーに属していないレコードの追加の試行 return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } TodoItem todoItem = todoItemDto.ToEntity(); // JSON シリアル化でループ参照の例外を回避するためにデタッチする必要があります db.Entry(todoList).State = EntityState.Detached; db.TodoItems.Add(todoItem); db.SaveChanges(); todoItemDto.TodoItemId = todoItem.TodoItemId; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todoItemDto); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = todoItemDto.TodoItemId })); return(response); }
// POST api/Todo public HttpResponseMessage PostTodoItem(TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } TodoList todoList = db.TodoLists.Find(todoItemDto.TodoListId); if (todoList == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (todoList.UserId != User.Identity.Name) { // Intentando agregar un registro que no pertenece al usuario return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } TodoItem todoItem = todoItemDto.ToEntity(); // Se debe desasociar para evitar una excepción de referencia en bucle durante la serialización de JSON db.Entry(todoList).State = EntityState.Detached; db.TodoItems.Add(todoItem); db.SaveChanges(); todoItemDto.TodoItemId = todoItem.TodoItemId; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todoItemDto); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = todoItemDto.TodoItemId })); return(response); }
/// <summary> /// Adds the to-do itemDto to the db /// </summary> /// <param name="itemDto"></param> /// <returns></returns> public async Task <TodoItemDto> AddItemAsync(TodoItemDto itemDto) { Guard.NotNull(itemDto, nameof(itemDto)); Guard.NotNull(itemDto.Description, nameof(itemDto.Description)); _logger.LogInformation($"In {nameof(AddItemAsync)}"); try { var todoItem = itemDto.ToEntity(); //set create time to now todoItem.CreateDateTime = DateTime.UtcNow; var status = todoItem.StatusHistory.First(); if (todoItem.StatusHistory == null || status == null) { status = new TodoItemStatus { Status = itemDto.CurrentStatus ?? StatusValues.New }; todoItem.StatusHistory = new List <TodoItemStatus> { status }; } status.StatusDateTime = todoItem.CreateDateTime; using (var context = TodoContextFactory.Create()) { var todoResult = await context.ToDoItems.AddAsync(todoItem); await context.SaveChangesAsync(); itemDto.CreateDateTime = todoResult.Entity.CreateDateTime; itemDto.ItemId = todoResult.Entity.ItemId; // set the status fields status = todoResult.Entity.StatusHistory.First(); itemDto.CurrentStatus = status.Status; itemDto.LastModifiedDateTime = status.StatusDateTime; } return(itemDto); } catch (Exception) { //log error throw; } finally { _logger.LogInformation($"Out {nameof(AddItemAsync)}"); } }
// PUT api/Todo/5 public HttpResponseMessage PutTodoItem(int id, TodoItemDto todoItemDto) { if (!ModelState.IsValid) { int x = 1; x = x + 1; return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != todoItemDto.TodoItemId) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } TodoItem todoItem = todoItemDto.ToEntity(); TodoList todoList = db.TodoLists.Find(todoItem.TodoListId); if (todoList == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (todoList.UserId != User.Identity.Name) { // Trying to modify a record that does not belong to the user return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } // Need to detach to avoid duplicate primary key exception when SaveChanges is called db.Entry(todoList).State = EntityState.Detached; db.Entry(todoItem).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
// PUT api/Todo/5 public HttpResponseMessage PutTodoItem(int id, TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != todoItemDto.TodoItemId) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } TodoItem todoItem = todoItemDto.ToEntity(); TodoList todoList = db.TodoLists.Find(todoItem.TodoListId); if (todoList == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (todoList.UserId != User.Identity.Name) { // 正在尝试修改不属于用户的记录 return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } // 需要分离,以避免在调用 SaveChanges 时发生重复主键异常 db.Entry(todoList).State = EntityState.Detached; db.Entry(todoItem).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
// PUT api/Todo/5 public HttpResponseMessage PutTodoItem(int id, TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != todoItemDto.TodoItemId) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } TodoItem todoItem = todoItemDto.ToEntity(); TodoList todoList = db.TodoLists.Find(todoItem.TodoListId); if (todoList == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (todoList.UserId != User.Identity.Name) { // ユーザーに属していないレコードの変更の試行 return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } // SaveChanges が呼び出されたときにプライマリ キーの例外の重複を回避するためにデタッチする必要があります db.Entry(todoList).State = EntityState.Detached; db.Entry(todoItem).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
// PUT api/Todo/5 public HttpResponseMessage PutTodoItem(int id, TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != todoItemDto.TodoItemId) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } TodoItem todoItem = todoItemDto.ToEntity(); TodoList todoList = db.TodoLists.Find(todoItem.TodoListId); if (todoList == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (todoList.UserId != User.Identity.Name) { // Intentando modificar un registro que no pertenece al usuario return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } // Se debe desasociar para evitar que se produzca una excepción de clave primaria duplicada cuando se llame a SaveChanges db.Entry(todoList).State = EntityState.Detached; db.Entry(todoItem).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
// PUT api/Todo/5 public HttpResponseMessage PutTodoItem(int id, TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != todoItemDto.TodoItemId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } TodoItem todoItem = todoItemDto.ToEntity(); TodoList todoList = db.TodoLists.Find(todoItem.TodoListId); if (todoList == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } if (todoList.UserId != User.Identity.Name) { // Trying to modify a record that does not belong to the user return Request.CreateResponse(HttpStatusCode.Unauthorized); } // Need to detach to avoid duplicate primary key exception when SaveChanges is called db.Entry(todoList).State = EntityState.Detached; db.Entry(todoItem).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } return Request.CreateResponse(HttpStatusCode.OK); }
// PUT api/Todo/5 public HttpResponseMessage PutTodoItem(int id, TodoItemDto todoItemDto) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != todoItemDto.TodoItemId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } TodoItem todoItem = todoItemDto.ToEntity(); TodoList todoList = db.TodoLists.Find(todoItem.TodoListId); if (todoList == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } if (todoList.UserId != User.Identity.Name) { // 사용자에게 속하지 않은 레코드를 수정하려고 시도합니다. return Request.CreateResponse(HttpStatusCode.Unauthorized); } // SaveChanges가 호출될 때 중복 기본 키 예외 발생을 방지하려면 분리해야 합니다. db.Entry(todoList).State = EntityState.Detached; db.Entry(todoItem).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } return Request.CreateResponse(HttpStatusCode.OK); }