public HttpResponseMessage Post(int id, RememberOptionModel model) { if (!ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors()); } var app = config.Applications.All.SingleOrDefault(x => x.ID == id); if (app == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } var rememberOption = RememberOptionParser.Parse(model); if (app.RememberOptions.Any(x => x.Value == rememberOption.Value)) { ModelState.AddModelError("", "That Remember Option already exists"); return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors()); } app.RememberOptions.Add(rememberOption); config.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK, new { rememberOption.ID, rememberOption.OptionLabel, rememberOption.Value }); }
public HttpResponseMessage Put(int id, RememberOptionModel model) { if (!ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors()); } var rememberOption = config.RememberOptions.All.SingleOrDefault(x => x.ID == id); if (rememberOption == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } var query = (from a in this.config.Applications.All from s in a.RememberOptions where s.ID == id select a); var app = query.Single(); var updatedRememberOption = RememberOptionParser.Parse(model); if (app.RememberOptions.Any(x => x.Value == updatedRememberOption.Value && x.ID != id)) { ModelState.AddModelError("", "A remember option with that value already exists"); return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors()); } rememberOption.OptionLabel = updatedRememberOption.OptionLabel; rememberOption.Value = updatedRememberOption.Value; config.SaveChanges(); return Request.CreateResponse(HttpStatusCode.NoContent); }
public static RememberOption Parse(RememberOptionModel model) { int expirationValue = 0; string timeUnit = null; switch (model.OptionSelect) { case 1: timeUnit = model.UserValue == 1 ? "hour" : "hours"; break; case 24: timeUnit = model.UserValue == 1 ? "day" : "days"; break; case 168: timeUnit = model.UserValue == 1 ? "week" : "weeks"; break; case 720: timeUnit = model.UserValue == 1 ? "month" : "months"; break; case 8640: timeUnit = model.UserValue == 1 ? "year" : "years"; break; case -1: expirationValue = model.OptionSelect; break; default: throw new Exception("Wrong expiration time unit"); } string rememberOptionLabel; if (model.OptionSelect != -1 && model.UserValue != null) { expirationValue = model.OptionSelect * (int) model.UserValue; rememberOptionLabel = model.UserValue + " " + timeUnit; } else { rememberOptionLabel = "Forever"; } return new RememberOption { OptionLabel = rememberOptionLabel, Value = expirationValue }; }