private CodeJewelModelFull DesirializeCodeJewelToFull(CodeJewel codeJewel) { double averageVote = 0; if (codeJewel.Votes.Count > 0) { averageVote = codeJewel.Votes.Average(v => v.VoteValue); } string category = "No Category"; if (codeJewel.Category != null) { category = codeJewel.Category.Name; } CodeJewelModelFull modelFull = new CodeJewelModelFull { AuthorName = codeJewel.AuthorMail, CodeJewel = codeJewel.SourceCode, Id = codeJewel.Id, AverageVote = averageVote, Category = category }; return(modelFull); }
// POST api/codejewels public HttpResponseMessage Post([FromBody] CodeJewelsModel value) { var codeJewel = new CodeJewel() { SourceCode = value.SourceCode, AuthorEmail = value.AuthorEmail, Rating = value.Rating }; if (value.CodeLanguage != null) { var category = new Category(); category.CodeLanguage = value.CodeLanguage; category.CodeJewels.Add(codeJewel); var entytiCategories = context.Set <Category>(); entytiCategories.Add(category); } else { this.entitySet.Add(codeJewel); } this.context.SaveChanges(); CodeJewelsModel result = new CodeJewelsModel() { Id = codeJewel.Id, SourceCode = codeJewel.SourceCode, AuthorEmail = codeJewel.AuthorEmail, Rating = codeJewel.Rating, CodeLanguage = value.CodeLanguage }; var response = Request.CreateResponse(HttpStatusCode.Created, result); return(response); }
// POST api/codejewels public HttpResponseMessage Post([FromBody] CodeJewel codeJewel) { return(this.ProcessAndHandleExceptions(() => { Validator.ValidateCodeJewel(codeJewel); var dbContext = new CodeJewelsDb(); GetOrCreateCategory(codeJewel, dbContext); dbContext.CodeJewels.Add(codeJewel); dbContext.SaveChanges(); var codeJewelDetails = new CodeJewelDetails { Id = codeJewel.Id, Category = codeJewel.Category.Name, AuthorEmail = codeJewel.AuthorEMail, Code = codeJewel.Code }; var response = this.Request.CreateResponse(HttpStatusCode.Created, codeJewelDetails); var location = new Uri(this.Url.Link("DefaultApi", new { id = codeJewel.Id })); response.Headers.Location = location; return response; })); }
// POST api/jewels public HttpResponseMessage PostCodeJewel(CodeJewelPostModel jewel) { if (ModelState.IsValid) { Category repositoryCategory = db.Categories.FirstOrDefault(c => c.Name == jewel.Category); if (repositoryCategory == null) { repositoryCategory = db.Categories.Add(new Category() { Name = jewel.Category }); db.SaveChanges(); } CodeJewel newJewel = db.CodeJewels.Add(new CodeJewel() { Category = repositoryCategory, AuthorEmail = jewel.AuthorEmail, SourceCode = jewel.SourceCode, }); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, jewel); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = newJewel.Id })); return(response); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
private void CheckRating(CodeJewelsContext context, CodeJewel codeJewel) { if (codeJewel.Rating.Average(x => x.Value) < 1) { context.CodeJewels.Remove(codeJewel); context.SaveChanges(); } }
public HttpResponseMessage GetDownVote([FromUri] int codeJewelId) { if (ModelState.IsValid) { CodeJewel jewel = this.db.CodeJewels.Include(cj => cj.Category).FirstOrDefault(cj => cj.Id == codeJewelId); if (jewel == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The jewel does not exist.")); } else { Vote vote = new Vote() { CodeJewel = jewel, Value = -1, }; Vote addedVote = db.Votes.Add(vote); jewel.Rating--; db.SaveChanges(); if (jewel.Rating <= -5) { var votesToRemove = db.Votes.Where(v => v.CodeJewel.Id == jewel.Id); foreach (var voteToRemove in votesToRemove) { db.Votes.Remove(voteToRemove); } db.CodeJewels.Remove(jewel); db.SaveChanges(); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The code jewel received too many down votes and has been deleted.")); } HttpResponseMessage response = Request.CreateResponse( HttpStatusCode.OK, new VoteModel() { CodeJewel = new CodeJewelGetModel() { Id = vote.CodeJewel.Id, AuthorEmail = vote.CodeJewel.AuthorEmail, Category = vote.CodeJewel.Category.Name, Rating = vote.CodeJewel.Rating, SourceCode = vote.CodeJewel.SourceCode }, Value = vote.Value }); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = addedVote.Id })); return(response); } } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
// PUT api/CodeJewels/5 public HttpResponseMessage Put(int id, [FromBody] CodeJewel value) { var entity = CodeJewelsRepository.Update(id, value); var responce = Request.CreateResponse(HttpStatusCode.OK, entity); responce.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.Id })); return(responce); }
private CodeJewelModel DesirializeCodeJewel(CodeJewel code) { CodeJewelModel model = new CodeJewelModel { AuthorName = code.AuthorMail, CodeJewel = code.SourceCode, Id = code.Id }; return(model); }
private void GetOrCreateCategory(CodeJewel codeJewel, CodeJewelsDb dbContext) { var categoryInDb = dbContext.Categories.FirstOrDefault(c => c.Name == codeJewel.Category.Name); if (categoryInDb != null) { codeJewel.Category = categoryInDb; } else { dbContext.Categories.Add(codeJewel.Category); dbContext.SaveChanges(); } }
// POST api/Code public HttpResponseMessage PostCodeJewel(CodeJewel codejewel) { if (ModelState.IsValid) { db.CodeJewels.Add(codejewel); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, codejewel); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = codejewel.Id })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
public HttpResponseMessage AddCodeJewel([FromBody] CodeJewel code) { var response = PerformOperation(() => { var context = new CodeContext(); using (context) { context.CodeJewels.Add(code); context.SaveChanges(); CodeJewelModel model = DesirializeCodeJewel(code); return(model); } }); return(response); }
public HttpResponseMessage CreateCodeJewel(CodeJewel codejewel) { if (ModelState.IsValid) { db.CodeJewels.Add(codejewel); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, codejewel); //response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = codejewel.Id })); return(response); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
public static void ValidateCodeJewel(CodeJewel codeJewel) { if (codeJewel == null) { throw new ArgumentNullException("codeJewel", "code jewel can't be null"); } ValidateCategory(codeJewel.Category); if (codeJewel.AuthorEMail == null) { throw new ArgumentNullException("AuthorEMail", "code jewel's authorEMail can't be null"); } if (codeJewel.Code == null) { throw new ArgumentNullException("Code", "code jewel's code can't be null"); } }
public HttpResponseMessage GetUpVote([FromUri] int codeJewelId) { if (ModelState.IsValid) { CodeJewel jewel = this.db.CodeJewels.Include(cj => cj.Category).FirstOrDefault(cj => cj.Id == codeJewelId); if (jewel == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } else { Vote vote = new Vote() { CodeJewel = jewel, Value = 1, }; Vote addedVote = db.Votes.Add(vote); jewel.Rating++; db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse( HttpStatusCode.OK, new VoteModel() { CodeJewel = new CodeJewelGetModel() { Id = vote.CodeJewel.Id, AuthorEmail = vote.CodeJewel.AuthorEmail, Category = vote.CodeJewel.Category.Name, Rating = vote.CodeJewel.Rating, SourceCode = vote.CodeJewel.SourceCode }, Value = vote.Value }); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = addedVote.Id })); return(response); } } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
// POST api/CodeJewels public HttpResponseMessage PostStudent(CodeJewelModel codeJewelModel) { if (codeJewelModel == null) { var errResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The supported code jewel model cannot be null"); throw new HttpResponseException(errResponse); } if (codeJewelModel.CategoryId == 0) { var errResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The category Id is not correct"); throw new HttpResponseException(errResponse); } DbCodeJewelsRepository codeJewelRepository = this.allRepositories.GetCodeJewelRepository(); CodeJewel codeJewel = new CodeJewel() { SourseCode = codeJewelModel.SourseCode, AuthorEmail = codeJewelModel.AuthorEmail, CategoryId = codeJewelModel.CategoryId }; codeJewelRepository.Add(codeJewel); CodeJewelModel createdCodeJewelModel = new CodeJewelModel() { CodeJewelId = codeJewel.CodeJewelId, SourseCode = codeJewel.SourseCode, AuthorEmail = codeJewel.AuthorEmail, Rating = codeJewel.Rating, CategoryId = codeJewel.CategoryId }; var response = Request.CreateResponse <CodeJewelModel>(HttpStatusCode.Created, createdCodeJewelModel); var resourceLink = Url.Link("DefaultApi", new { id = createdCodeJewelModel.CodeJewelId }); response.Headers.Location = new Uri(resourceLink); return(response); }
// POST api/codejewels public HttpResponseMessage Post(CodeJewel value) { var added = this.dbContext.CodeJewels.Add(value); HttpResponseMessage responce; try { this.dbContext.SaveChanges(); responce = Request.CreateResponse<CodeJewel>(HttpStatusCode.Created, added); } catch (Exception ex) { responce = Request.CreateResponse<CodeJewel>(HttpStatusCode.InternalServerError, added); this.dbContext.CodeJewels.Local.Clear(); } var resourceLink = Url.Link("DefaultApi", new { id = added.Id }); responce.Headers.Location = new Uri(resourceLink); return responce; }
// POST api/codejewels public HttpResponseMessage Post(CodeJewel value) { var added = this.dbContext.CodeJewels.Add(value); HttpResponseMessage responce; try { this.dbContext.SaveChanges(); responce = Request.CreateResponse <CodeJewel>(HttpStatusCode.Created, added); } catch (Exception ex) { responce = Request.CreateResponse <CodeJewel>(HttpStatusCode.InternalServerError, added); this.dbContext.CodeJewels.Local.Clear(); } var resourceLink = Url.Link("DefaultApi", new { id = added.Id }); responce.Headers.Location = new Uri(resourceLink); return(responce); }
// POST api/codejewels public HttpResponseMessage Post([FromBody]CodeJewelsModel value) { var codeJewel = new CodeJewel() { SourceCode = value.SourceCode, AuthorEmail = value.AuthorEmail, Rating = value.Rating }; if (value.CodeLanguage != null) { var category = new Category(); category.CodeLanguage = value.CodeLanguage; category.CodeJewels.Add(codeJewel); var entytiCategories = context.Set<Category>(); entytiCategories.Add(category); } else { this.entitySet.Add(codeJewel); } this.context.SaveChanges(); CodeJewelsModel result = new CodeJewelsModel() { Id = codeJewel.Id, SourceCode = codeJewel.SourceCode, AuthorEmail = codeJewel.AuthorEmail, Rating = codeJewel.Rating, CodeLanguage = value.CodeLanguage }; var response = Request.CreateResponse(HttpStatusCode.Created, result); return response; }