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));
                }
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        // POST api/Votes/5
        public HttpResponseMessage Post(int id, [FromBody] Vote value)
        {
            using (var context = new CodeJewelsContext())
            {
                if (value.Value >= 0 && value.Value <= 5)
                {
                    var codeJewel = context.CodeJewels.Where(x => x.Id == id).FirstOrDefault();

                    if (codeJewel != null)
                    {
                        codeJewel.Rating.Add(value);

                        CheckRating(context, codeJewel);

                        context.SaveChanges();
                    }
                    else
                    {
                        throw new ArgumentException("No such code jewel!");
                    }
                }
                else
                {
                    throw new ArgumentException("Value must be between 0 and 5!");
                }
            }


            var responce = Request.CreateResponse(HttpStatusCode.OK, value);

            return(responce);
        }
        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 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);
        }
Exemplo n.º 6
0
 private void CheckRating(CodeJewelsContext context, CodeJewel codeJewel)
 {
     if (codeJewel.Rating.Average(x => x.Value) < 1)
     {
         context.CodeJewels.Remove(codeJewel);
         context.SaveChanges();
     }
 }
        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 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);
        }
Exemplo n.º 11
0
        public CodeJewelsController()
        {
            var dbContext = new CodeJewelsContext();

            this.CodeJewelsRepository = new DbCodeJewelsRepository(dbContext);
        }
Exemplo n.º 12
0
 public CategoryJewelsController(CodeJewelsContext dbContext)
 {
     this.dbContext = dbContext;
 }