Exemplo n.º 1
0
        private void CheckForVeryLowAvgVote(int id, Vote vote, CodeContext context)
        {
           // var context = new CodeContext();
            var jewel = context.CodeJewels.Include("Votes").FirstOrDefault(j => j.Id == id);
            if (jewel != null)
            {
                double avgVote = 0;
                if (jewel.Votes.Count > 0)
                {
                    avgVote = jewel.Votes.Average(v => v.VoteValue);
                }

                if (avgVote < MINVOTEVALUE)
                {
                    context.Votes.Remove(vote);
                    context.CodeJewels.Remove(jewel);
                    context.SaveChanges();
                }
            }
            else
            {
                throw new InvalidOperationException("Trying to delete, but didn't find codeJewel");
            }

        }
Exemplo n.º 2
0
  public HttpResponseMessage AddCodeJewel([FromBody] CodeJewel code)
 {
     var response = PerformOperation(() =>
         {
             var context = new CodeContext();
             using (context)
             {
                 context.CodeJewels.Add(code);
                 context.SaveChanges();
                 CodeJewelModel model = DesirializeCodeJewel(code);
                 return model;
             }
         });
     return response;
 }
Exemplo n.º 3
0
        private void ChechForVeryWeekAvgVote(int id, Vote vote, CodeContext context)
        {
            // var context = new CodeContext();
            var jewel = context.CodeJewels.Include("Votes").FirstOrDefault(j => j.Id == id);
            if (jewel != null)
            {
                double avgVote = 0;
                if (jewel.Votes.Count > 0)
                {
                    avgVote = jewel.Votes.Average(v => v.VoteValue);
                }

                if (avgVote < MINVOTEVALUE)
                {
                    context.Votes.Remove(vote);
                    context.CodeJewels.Remove(jewel);
                    context.SaveChanges();
                    //DbEntityEntry entry = context.Entry(vote);
                    //if (entry.State != EntityState.Deleted)
                    //{
                    //    entry.State = EntityState.Deleted;
                    //}
                    //else
                    //{
                    //    context.Votes.Attach(vote);
                    //    context.Votes.Remove(vote);
                    //}

                    //DbEntityEntry entryJewel = context.Entry(jewel);
                    //if (entryJewel.State != EntityState.Deleted)
                    //{
                    //    entryJewel.State = EntityState.Deleted;
                    //}
                    //else
                    //{
                    //    context.CodeJewels.Attach(jewel);
                    //    context.CodeJewels.Remove(jewel);
                    //}

                    //context.SaveChanges();
                }
            }
            else
            {
                throw new InvalidOperationException("Trying to delete, but didn't find codeJewel");
            }
        }
Exemplo n.º 4
0
        static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<CodeContext, Configuration>());
            var context = new CodeContext();
            var cat = new Category
            {
                Name = "Category 1"
            };
            context.Categories.Add(cat);
            context.SaveChanges();

            var cats = from c in context.Categories
                       select c;
            foreach (var c in cats)
            {
                Console.WriteLine(c.Id);
                Console.WriteLine(c.Name);
            }
        }
Exemplo n.º 5
0
        public HttpResponseMessage AddVote(int id, [FromBody] Vote vote)
        {
            var response = PerformOperation(() =>
            {
                var context = new CodeContext();
                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();
                    ChechForVeryWeekAvgVote(id, vote, context);

                    return vote;
                }
            });
            return response;
        }