private void SetupDomainObject() { var todoId = Guid.NewGuid(); var categoryId = Guid.NewGuid(); var userId = Guid.NewGuid().ToString(); var dueOn = DateTime.Now; _domainTodo = new Todo() { Id = todoId, CategoryId = categoryId, Description = "Todo1", Location = "Costco", UserId = userId, DueOn = dueOn, IsComplete = false }; }
/// <summary> /// Add a record the database. /// </summary> public void Add(Todo todo) { if (todo == null) { _logger.Error("Empty Todo Item"); throw new ArgumentNullException(nameof(todo)); } _context.Todos.Add(todo); _context.SaveChanges(_userName); }
/// <summary> /// Convert the TodoDomainModel object to ViewModel. This is used when we are reading todos from the database. /// </summary> /// <param name="domainModel"></param> /// <returns></returns> public static TodoViewModel MapDomainModelToViewModel(Todo domainModel) { return new TodoViewModel() { Description = domainModel.Description, CategoryId = domainModel.CategoryId, DueOn = domainModel.DueOn, Id = domainModel.Id, IsComplete = domainModel.IsComplete, Location = domainModel.Location, UserId = domainModel.UserId, //This property determines whether the todoitem is overdue. Not stored in the database as it is computed from DueOn property. IsDueToday = domainModel.DueOn.Date <= DateTime.Today.Date, }; }
/// <summary> /// Updates a record. If none found throw TodoNotFoundException. /// </summary> public void Update(Todo todo) { if (todo == null) { _logger.Error("Empty Todo Item"); throw new ArgumentNullException(nameof(todo)); } if (!_context.Todos.Any(x => x.Id == todo.Id)) { _logger.Error("Cannot find todo in the database"); throw new TodoNotFoundException($"Cannot find Todo item with id {todo.Id}"); } else { _context.Entry(todo).State = EntityState.Modified; _context.Todos.AddOrUpdate(todo); _context.SaveChanges(_userName); } }
public IHttpActionResult PutTodo(Guid id ,[FromBody] TodoViewModel todo) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { if (todo == null || id != todo.Id) { return BadRequest(); } var domainTodo = new Todo() { Description = todo.Description, CategoryId = todo.CategoryId, DueOn = todo.DueOn, Id = id, IsComplete = todo.IsComplete, Location = todo.Location, UserId = todo.UserId }; _todoService.Update(domainTodo); return StatusCode(HttpStatusCode.NoContent); } catch (TodoNotFoundException) { _logger.Debug($"Exception Todo Id - {id} does not exist."); return NotFound(); } catch(ArgumentException) { return BadRequest(); } }
/// <summary> /// Update a todo record with values as in todo record argument. /// </summary> public void Update(Todo todo) { if(todo == null) throw new ArgumentNullException(nameof(todo)); _todoRepository.Update(todo); }
public HttpResponseMessage PostTodo(TodoViewModel todo) { if (!ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.BadRequest); } var domainTodo = new Todo() { Description = todo.Description, CategoryId = todo.CategoryId, Location = todo.Location, DueOn = todo.DueOn, UserId = User.Identity.GetUserId() }; _todoService.Add(domainTodo); return Request.CreateResponse(HttpStatusCode.OK, todo); }