public HttpResponseMessage AddCodeJewel([FromBody] CodeJewel code) { var response = PerformOperation(() => { var context = new CodeJewelEntities(); using (context) { context.CodeJewels.Add(code); context.SaveChanges(); CodeJewelModel model = DesirializeCodeJewel(code); return(model); } }); return(response); }
public HttpResponseMessage SearchByCriteriaCategory(string category) { var response = PerformOperation(() => { var context = new CodeJewelEntities(); using (context) { var jewels = context.CodeJewels.Include("Category").Include("Votes").Where(code => code.Category.Name == category); HashSet <CodeJewelModel> models = new HashSet <CodeJewelModel>(); foreach (var codeJewel in jewels) { models.Add(DesirializeCodeJewelToFull(codeJewel)); } return(models); } }); return(response); }
public HttpResponseMessage GetAllCodeJewels() { var response = PerformOperation(() => { var context = new CodeJewelEntities(); using (context) { var jewels = context.CodeJewels; HashSet <CodeJewelModel> models = new HashSet <CodeJewelModel>(); foreach (var codeJewel in jewels) { models.Add(DesirializeCodeJewel(codeJewel)); } return(models); } }); return(response); }
public HttpResponseMessage AddVote(int id, [FromBody] Vote vote) { var response = PerformOperation(() => { var context = new CodeJewelEntities(); using (context) { var jewel = context.CodeJewels.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(); return(vote); } }); return(response); }