public HttpResponseMessage PostRecord(RecordPostModel model, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var user = this.Data.Users.All().FirstOrDefault( x => x.SessionKey == sessionKey); if (user == null || sessionKey == string.Empty) { throw new InvalidOperationException("Invalid sessionKey"); } double categoryRank; var category = this.Data.Records.All().Where(x => x.UserId == user.Id). Select(x => x.Category).OrderByDescending(x => x.Rank).FirstOrDefault(); if (category != null) { categoryRank = category.Rank; } else { categoryRank = 0; } var nextCategory = this.Data.Categories.All(). Where(x => x.Level.Name == model.Level).FirstOrDefault(x => x.Rank == categoryRank + 1); var responseModel = new RecordResponseModel() { Id = null }; if (nextCategory != null) { var record = new Record() { Category = nextCategory, User = user, CoverLevel = this.Data.CoverLevels.All().Where(x => x.State == StateFail).FirstOrDefault() }; this.Data.Records.Add(record); this.Data.SaveChanges(); responseModel.Id = record.Id.ToString(); } var response = this.Request.CreateResponse(HttpStatusCode.OK, responseModel); return(response); }); return(responseMsg); }
public JsonResult Post([FromBody] RecordPostModel recordModel) { // Admin and Client can access if (AuthRepo.IsAuthenticated()) { User currentUser = AppRepo.GetUserById(AuthRepo.CurrentUserId); int status; string msg; AppRepo.AddRecord(currentUser, recordModel, out status, out msg); Response.StatusCode = status; return(Json(new { Msg = msg })); } Response.StatusCode = 401; return(Json(new { Msg = "Unauthorized" })); }
public void AddRecord(User owner, RecordPostModel recordPost, out int statusCode, out string msg) { Record newRecord = recordPost.toRecord(owner); Context.Records.Add(newRecord); try { Context.SaveChanges(); } catch (Exception) { statusCode = 400; msg = "Record already exists"; return; } statusCode = 200; msg = "ok"; }