public HttpResponseMessage Vote(int id, int voteValue) { if(voteValue != 1 && voteValue != -1) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid vote value"); } CodeJewelsContext context = new CodeJewelsContext(); using (context) { try { var jewel = context.Jewels.Find(id); jewel.Rating += voteValue; if(jewel.Rating < MIN_ACCEPTABLE_RATING) { context.Jewels.Remove(jewel); } context.SaveChanges(); return Request.CreateResponse(HttpStatusCode.Created); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); } } }
public HttpResponseMessage Add(int id, [FromBody] Vote vote) { var response = this.PerformOperation(() => { var context = new CodeJewelsContext(); using (context) { var jewel = context.CodeJewels.Find(id); if (jewel == null) { throw new ArgumentNullException("Cannot find jewel"); } jewel.Votes.Add(vote); context.Votes.Add(vote); context.SaveChanges(); if (jewel.Votes.Count > 0) { double averageVote = jewel.Votes.Average(v => v.Value); if (averageVote < MIN_RATING) { context.Votes.Remove(vote); context.CodeJewels.Remove(jewel); context.SaveChanges(); } } return vote; } }); return response; }
public IEnumerable<Jewel> GetAll() { CodeJewelsContext context = new CodeJewelsContext(); using (context) { return context.Jewels.ToList(); } }
public IEnumerable<Jewel> GetBySourceCodeAndCategory(string sourceCode, string category) { CodeJewelsContext context = new CodeJewelsContext(); using (context) { return context.Jewels .Where(j => j.SourceCode.Contains(sourceCode) && j.Category == category) .ToList(); } }
public HttpResponseMessage PostJewel([FromBody] Jewel value) { CodeJewelsContext context = new CodeJewelsContext(); using (context) { context.Jewels.Add(value); context.SaveChanges(); return Request.CreateResponse(HttpStatusCode.Created); } }
public HttpResponseMessage GetAll() { var response = this.PerformOperation(() => { var context = new CodeJewelsContext(); using (context) { var jewels = context.CodeJewels.ToList(); return jewels; } }); return response; }
public HttpResponseMessage AddCodeJewel([FromBody] CodeJewel jewel) { var response = this.PerformOperation(() => { var context = new CodeJewelsContext(); using (context) { context.CodeJewels.Add(jewel); context.SaveChanges(); return jewel; } }); return response; }
public HttpResponseMessage GetBySource(string source) { var response = this.PerformOperation(() => { var context = new CodeJewelsContext(); using (context) { var jewels = context.CodeJewels .Include("Votes") .Include("Categories") .Where(s => s.Source == source).ToList(); return jewels; } }); return response; }
public HttpResponseMessage GetByCategory(string category) { var response = this.PerformOperation(() => { var context = new CodeJewelsContext(); using (context) { var jewels = context.CodeJewels .Include("Votes") .Include("Categories") .Where(c => c.Category.Name == category).ToList(); return jewels; } }); return response; }
public CodeJewelsController(CodeJewelsContext dbContext) { this.dbContext = dbContext; }