public async Task <HttpResponseMessage> Put(int id, [FromBody] TaskWorkVO vo) { try { await service.UpdateAsync(id, vo); return(Request.CreateResponse(HttpStatusCode.OK, "Tarefa atualizada com sucesso.")); } catch (EntityNotFoundException) { Console.WriteLine("Task not found"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Ocorreu um erro ao salvar a tarefa.")); } }
/// <summary> /// This method edits an existing task work. /// </summary> /// <param name="id">Task work id.</param> /// <param name="vo">Task work object to be edited.</param> public async Task UpdateAsync(int id, TaskWorkVO vo) { using (var db = new PoWDbContext()) { var entity = await db.TaskWorks.Where(tw => tw.Id == id).SingleOrDefaultAsync(); if (entity == null) { throw new EntityNotFoundException(); } switch (vo.Status) { case TaskWorkStatus.UNKNOW: throw new System.Exception("Status da tarefa inválido."); case TaskWorkStatus.TODO: case TaskWorkStatus.DOING: entity.EditDate = DateTime.Now; break; case TaskWorkStatus.DONE: entity.EditDate = DateTime.Now; entity.CloseDate = DateTime.Now; break; case TaskWorkStatus.REMOVED: entity.EditDate = DateTime.Now; entity.RemoveDate = DateTime.Now; break; default: break; } entity.Title = vo.Title; entity.Description = vo.Description; entity.Status = vo.Status; await db.SaveChangesAsync(); } }