Exemplo n.º 1
0
        // POST api/jewels
        public HttpResponseMessage PostCodeJewel(CodeJewelPostModel jewel)
        {
            if (ModelState.IsValid)
            {
                Category repositoryCategory = db.Categories.FirstOrDefault(c => c.Name == jewel.Category);
                if (repositoryCategory == null)
                {
                    repositoryCategory = db.Categories.Add(new Category()
                    {
                        Name = jewel.Category
                    });
                    db.SaveChanges();
                }

                CodeJewel newJewel = db.CodeJewels.Add(new CodeJewel()
                {
                    Category    = repositoryCategory,
                    AuthorEmail = jewel.AuthorEmail,
                    SourceCode  = jewel.SourceCode,
                });
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, jewel);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = newJewel.Id }));
                return(response);
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage UpFoteCodeJewel(int codeJewelId)
        {
            var codejewel = db.CodeJewels.FirstOrDefault(cj => cj.Id == codeJewelId);

            if (codejewel != null)
            {
                codejewel.Rating += 1;
                db.SaveChanges();
            }

            return(Request.CreateResponse(HttpStatusCode.NoContent));
        }
        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.º 4
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.º 5
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);
        }
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();
     }
 }
Exemplo n.º 7
0
        public HttpResponseMessage GetUpVote([FromUri] int codeJewelId)
        {
            if (ModelState.IsValid)
            {
                CodeJewel jewel = this.db.CodeJewels.Include(cj => cj.Category).FirstOrDefault(cj => cj.Id == codeJewelId);
                if (jewel == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
                }
                else
                {
                    Vote vote = new Vote()
                    {
                        CodeJewel = jewel,
                        Value     = 1,
                    };
                    Vote addedVote = db.Votes.Add(vote);
                    jewel.Rating++;
                    db.SaveChanges();

                    HttpResponseMessage response = Request.CreateResponse(
                        HttpStatusCode.OK,
                        new VoteModel()
                    {
                        CodeJewel = new CodeJewelGetModel()
                        {
                            Id          = vote.CodeJewel.Id,
                            AuthorEmail = vote.CodeJewel.AuthorEmail,
                            Category    = vote.CodeJewel.Category.Name,
                            Rating      = vote.CodeJewel.Rating,
                            SourceCode  = vote.CodeJewel.SourceCode
                        },
                        Value = vote.Value
                    });
                    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = addedVote.Id }));
                    return(response);
                }
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
        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);
        }