Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }