コード例 #1
0
        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;
        }
コード例 #2
0
        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;
        }
コード例 #3
0
        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;
        }
コード例 #4
0
        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;
        }
コード例 #5
0
        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;
        }
コード例 #6
0
        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;
        }
コード例 #7
0
        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;
        }
コード例 #8
0
        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;
        }