public object Patch(League request) { var fields = Request.QueryString["fields"].Split(new[] { ',' }); if (!fields.Any()) throw new Exception("You must provide the fields to update via ?fields= in querystring"); Db.UpdateOnly(request, delegate(SqlExpression<League> expression) { foreach (var field in fields) { var match = ModelDefinition<League>.Definition.FieldDefinitions.FirstOrDefault(x => x.FieldName.ToLower() == field.ToLower()); if (match != null) expression.UpdateFields.Add(match.FieldName); } return expression.Where(x => x.Id == request.Id); }); // returning entire object. You may want to do a different response code return Db.SingleById<League>(request.Id); }
public object Post(League request) { Db.Insert(request); return Db.SingleById<League>(Db.LastInsertId()); }
// You can now use DTOs that do not adhere to IReturn. Cool! public object Get(League request) { return Db.SingleById<League>(request.Id); }
public void CanPerform_PartialUpdate() { var client = new JsonServiceClient("http://localhost:53990/api/"); // back date for readability var created = DateTime.Now.AddHours(-2); // Create a record so we can patch it var league = new League() {Name = "BEFORE", Abbreviation = "BEFORE", DateUpdated = created, DateCreated = created}; var newLeague = client.Post<League>(league); // Update Name and DateUpdated fields. Notice I don't want to update DateCreatedField. // I also added a fake field to show it does not cause any errors var updated = DateTime.Now; newLeague.Name = "AFTER"; newLeague.Abbreviation = "AFTER"; // setting to after but it should not get updated newLeague.DateUpdated = updated; client.Patch<League>("http://localhost:53990/api/leagues/" + newLeague.Id + "?fields=Name,DateUpdated,thisFieldDoesNotExist", newLeague); var updatedLeague = client.Get<League>(newLeague); Assert.AreEqual(updatedLeague.Name, "AFTER"); Assert.AreEqual(updatedLeague.Abbreviation, "BEFORE"); Assert.AreEqual(updatedLeague.DateUpdated.ToString(), updated.ToString(), "update fields don't match"); Assert.AreEqual(updatedLeague.DateCreated.ToString(), created.ToString(), "created fields don't match"); // double check Assert.AreNotEqual(updatedLeague.DateCreated, updatedLeague.DateUpdated); }