public HttpResponseMessage SearchByCriteriaCategory(string category) { var responses = ExceptionHandler(() => { var context = new CodeJewelsContext(); using (context) { var jewels = context.Codes.Include("Category").Include("Votes").Where(code => code.Category.Name == category); var models = (from c in jewels select new CodeModel { Id = c.Id, SourseCode = c.SourceCode, Category = c.Category.Name }); var response = this.Request.CreateResponse(HttpStatusCode.Created, models); return response; } }); return responses; }
public HttpResponseMessage AddVote(int id, [FromBody] Vote vote) { var responses = ExceptionHandler(() => { var context = new CodeJewelsContext(); using (context) { var jewel = context.Codes.FirstOrDefault(j => j.Id == id); if (jewel == null) { throw new InvalidOperationException("The jewel does not exists!"); } jewel.Votes.Add(vote); context.Votes.Add(vote); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created, vote); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = vote.Id })); return response; } }); return responses; }
public HttpResponseMessage GetAllCodeJewels() { var responses = ExceptionHandler(() => { var context = new CodeJewelsContext(); using (context) { var jewels = context.Codes; var models = (from c in jewels select new CodeModel { Id = c.Id, SourseCode = c.SourceCode, Category = c.Category.Name }); var response = this.Request.CreateResponse(HttpStatusCode.Created, models); return response; } }); return responses; }
public IQueryable<JewelModel> GetCodeJewels(string category, string source) { var responseMsg = PerformOperationAndHandleExceptions(() => { var dbContext = new CodeJewelsContext(); var jewels = dbContext.Jewels.Include("Category").Include("Votes").Where(j => j.SourceCode.Contains(source) && j.Category.Name == category); return from jewel in jewels select new JewelModel() { Id = jewel.Id, AuthorMail = jewel.AuthorMail, SourceCode = jewel.SourceCode }; }); return responseMsg; }
public HttpResponseMessage AddCodeJewel([FromBody] Code code) { var responses = ExceptionHandler(() => { var context = new CodeJewelsContext(); using (context) { context.Codes.Add(code); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created, code); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = code.Id })); return response; } }); return responses; }
public HttpResponseMessage PostAddCategory([FromBody] Category category) { var responseMsg = PerformOperationAndHandleExceptions(() => { using (var dbContext = new CodeJewelsContext()) { if (category == null) { throw new InvalidOperationException("The category must not be null!"); } dbContext.Categories.Add(category); dbContext.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created, category); return response; } }); return responseMsg; }
public HttpResponseMessage PostAddVote(int jewelId, [FromBody] Vote vote) { var responseMsg = PerformOperationAndHandleExceptions(() => { using (var dbContext = new CodeJewelsContext()) { Jewel jewel = dbContext.Jewels.FirstOrDefault(j => j.Id == jewelId); if (jewel == null) { throw new InvalidOperationException("The jewel does not exist!"); } jewel.Votes.Add(vote); dbContext.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created, vote); return response; } }); return responseMsg; }
public HttpResponseMessage PostCodeJewel([FromBody] JewelModel jewel) { var responseMsg = PerformOperationAndHandleExceptions(() => { using (var dbContext = new CodeJewelsContext()) { if (jewel == null) { throw new InvalidOperationException("The jewel must not be null!"); } Jewel jewelEntity = new Jewel() { Id = jewel.Id, AuthorMail = jewel.AuthorMail, SourceCode = jewel.SourceCode }; dbContext.Jewels.Add(jewelEntity); dbContext.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created, jewel); return response; } }); return responseMsg; }