Esempio n. 1
0
        public HttpResponseMessage Add(int id, [FromBody] Vote vote)
        {
            var response = this.PerformOperation(() =>
            {
                var context = new CodeJewelsContext();
                using (context)
                {
                    var jewel = context.CodeJewels.Find(id);
                    if (jewel == null)
                    {
                        throw new ArgumentNullException("Cannot find jewel");
                    }
                    jewel.Votes.Add(vote);
                    context.Votes.Add(vote);
                    context.SaveChanges();

                    if (jewel.Votes.Count > 0)
                    {
                        double averageVote = jewel.Votes.Average(v => v.Value);
                        if (averageVote < MIN_RATING)
                        {
                            context.Votes.Remove(vote);
                            context.CodeJewels.Remove(jewel);
                            context.SaveChanges();
                        }
                    }

                    return vote;
                }
            });

            return response;
        }
        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);
                }
            }
        }
        public HttpResponseMessage PostJewel([FromBody] Jewel value)
        {
            CodeJewelsContext context = new CodeJewelsContext();
            using (context)
            {
                context.Jewels.Add(value);
                context.SaveChanges();

                return Request.CreateResponse(HttpStatusCode.Created);
            }
        }
Esempio n. 4
0
        public HttpResponseMessage AddCodeJewel([FromBody] CodeJewel jewel)
        {
            var response = this.PerformOperation(() =>
            {
                var context = new CodeJewelsContext();
                using (context)
                {
                    context.CodeJewels.Add(jewel);
                    context.SaveChanges();
                    return jewel;
                }
            });

            return response;
        }