public void Put(string id, Todo value) { if (!_todos.UpdateTodo(id, value)) { throw new HttpResponseException(HttpStatusCode.NotFound); } }
public TodoRepository(string connection) { if (string.IsNullOrWhiteSpace(connection)) { connection = ConfigurationManager.AppSettings["TodoConnectionString"]; } _server = MongoServer.Create(connection); _database = _server.GetDatabase(ConfigurationManager.AppSettings["TodoDatabaseName"], SafeMode.True); _todos = _database.GetCollection<Todo>(ConfigurationManager.AppSettings["TodoTableName"]); bool resetData = true; bool.TryParse(ConfigurationManager.AppSettings["ResetData"], out resetData); if (resetData) { _todos.RemoveAll(); for (int i = 0; i < 5; i++) { Todo todo = new Todo { Description = string.Format("Todo {0}", i + 1), Done = (i % 3 == 0 ? true : false), CreationDatetime = DateTime.Now, DoneDatetime = null }; if (todo.Done) todo.DoneDatetime = DateTime.Now; AddTodo(todo); } } }
public Todo AddTodo(Todo item) { if (item != null) { item.Id = ObjectId.GenerateNewId().ToString(); item.CreationDatetime = DateTime.UtcNow; _todos.Insert(item); } return item; }
public Todo Post(Todo value) { Todo todo = _todos.AddTodo(value); return todo; }
public bool UpdateTodo(string id, Todo item) { IMongoQuery query = Query.EQ("_id", id); IMongoUpdate update = Update .Set("Description", item.Description) .Set("CreationDatetime", item.CreationDatetime) .Set("Done", item.Done) .Set("DoneDatetime", item.DoneDatetime); SafeModeResult result = _todos.Update(query, update); return result.UpdatedExisting; }