Exemplo n.º 1
0
        public async Task <int> AddExerciseToTrainingAsync(ExerciseWithTraining exercise)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                bool exists = ctx.Exercise.Any(e => e.Id == exercise.Id);
                int  exerciseId;
                if (exists)
                {
                    exerciseId = exercise.Id;
                }
                else
                {
                    await ctx.Exercise.AddAsync(new Exercise
                    {
                        Description = exercise.Description,
                        Title       = exercise.Title
                    });

                    await ctx.SaveChangesAsync();

                    exerciseId = ctx.Exercise.ToList().Last().Id;
                }
                await ctx.TrainingExercises.AddAsync(new TrainingExercise
                {
                    ExerciseId = exerciseId,
                    TrainingId = exercise.TrainingId
                });

                await ctx.SaveChangesAsync();

                return(exerciseId);
            }
        }
Exemplo n.º 2
0
        public async Task <List <int> > AddMessageAsync(MessageSocketsModel message)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                List <int> ints = new List <int>();
                if (message.HasImage && message.Content == null)
                {
                    message.Content = "";
                }
                await ctx.Messages.AddAsync(new Message
                {
                    Content    = message.Content,
                    HasImage   = message.HasImage,
                    ReceiverId = message.ReceiverId,
                    SenderId   = message.SenderId,
                    TimeStamp  = DateTime.Now
                });

                await ctx.SaveChangesAsync();

                ints.Add(ctx.Messages.ToList().Last().Id);

                await ctx.Notifications.AddAsync(new Notification {
                    SenderId         = message.SenderId,
                    ReceiverId       = message.ReceiverId,
                    NotificationType = ActionType.MESSAGE_CREATE.ToString()
                });

                await ctx.SaveChangesAsync();

                ints.Add(ctx.Notifications.ToList().Last().Id);

                return(ints);
            }
        }
Exemplo n.º 3
0
        public async Task <int> AddMealToDietAsync(MealWithDiet meal)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                bool exists = ctx.Meal.Any(m => m.Id == meal.Id);
                int  mealId;
                if (exists)
                {
                    mealId = meal.Id;
                }
                else
                {
                    await ctx.Meal.AddAsync(new Meal
                    {
                        Title       = meal.Title,
                        Description = meal.Description,
                        Calories    = meal.Calories
                    });

                    await ctx.SaveChangesAsync();

                    mealId = ctx.Meal.ToList().Last().Id;
                }

                await ctx.DietMeals.AddAsync(new DietMeal
                {
                    MealId = mealId,
                    DietId = meal.DietId
                });

                await ctx.SaveChangesAsync();

                return(mealId);
            }
        }
Exemplo n.º 4
0
        public async Task <bool> EditMealInDiet(MealWithDiet meal)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                int  mealFrequency = GetNumberOfDietsForMeal(meal.Id);
                Meal mealDb        = await ctx.Meal.FirstAsync(m => m.Id == meal.Id);

                if (mealFrequency > 1)
                {
                    await ctx.Meal.AddAsync(new Meal
                    {
                        Calories    = mealDb.Calories,
                        Description = mealDb.Description,
                        Title       = mealDb.Title
                    });

                    await ctx.SaveChangesAsync();

                    int mealId = ctx.Meal.ToList().Last().Id;
                    Console.WriteLine("New meal id is " + mealId);
                    var dietMeals = ctx.DietMeals.Where(dm => dm.MealId == meal.Id &&
                                                        dm.Diet.Id != meal.DietId).ToList();
                    foreach (var dietMeal in dietMeals)
                    {
                        ctx.DietMeals.Remove(dietMeal);
                        await ctx.DietMeals.AddAsync(new DietMeal
                        {
                            DietId = dietMeal.DietId,
                            MealId = mealId
                        });
                    }
                }

                if (meal.Calories >= 0)
                {
                    mealDb.Calories = meal.Calories;
                }
                if (!string.IsNullOrEmpty(meal.Description))
                {
                    mealDb.Description = meal.Description;
                }
                if (!string.IsNullOrEmpty(meal.Title))
                {
                    mealDb.Title = meal.Title;
                }

                try
                {
                    ctx.Meal.Update(mealDb);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    return(false);
                }

                return(true);
            }
        }
Exemplo n.º 5
0
        public async Task <int> AddTrainingAsync(TrainingSocketsModelWithOwner training)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    User owner = await ctx.Users.FirstAsync(u => u.Id == training.Owner.UserId);

                    Training trainingDb = new Training
                    {
                        Title     = training.Title,
                        TimeStamp = training.TimeStamp,
                        Duration  = training.Duration,
                        IsPublic  = training.Global,
                        Type      = training.Type,
                        Owner     = owner
                    };
                    await ctx.Training.AddAsync(trainingDb);

                    await ctx.SaveChangesAsync();

                    int createdTrainingId = ctx.Training.ToList().Last().Id;

                    if (training.Exercises != null && training.Exercises.Any())
                    {
                        foreach (var exercise in training.Exercises)
                        {
                            int createdExerciseId;
                            if (exercise.Id > 0)
                            {
                                createdExerciseId = exercise.Id;
                            }
                            else
                            {
                                await ctx.Exercise.AddAsync(exercise);

                                await ctx.SaveChangesAsync();

                                createdExerciseId = ctx.Exercise.ToList().Last().Id;
                            }

                            await ctx.TrainingExercises.AddAsync(new TrainingExercise
                            {
                                ExerciseId = createdExerciseId,
                                TrainingId = createdTrainingId
                            });

                            await ctx.SaveChangesAsync();
                        }
                    }

                    return(createdTrainingId);
                }
                catch (Exception e)
                {
                    return(-1);
                }
            }
        }
Exemplo n.º 6
0
        public async Task <int> AddDietAsync(DietSocketsModelWithOwner diet)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    User owner = await ctx.Users.FirstAsync(u => u.Id == diet.Owner.UserId);

                    Diet dietDb = new Diet
                    {
                        Description = diet.Description,
                        IsPublic    = diet.Global,
                        Owner       = owner,
                        Title       = diet.Title
                    };
                    await ctx.Diet.AddAsync(dietDb);

                    await ctx.SaveChangesAsync();

                    int createdDietId = ctx.Diet.ToList().Last().Id;

                    if (diet.Meals != null && diet.Meals.Any())
                    {
                        foreach (var meal in diet.Meals)
                        {
                            int createdMealId;
                            if (meal.Id > 0)
                            {
                                createdMealId = meal.Id;
                            }
                            else
                            {
                                await ctx.Meal.AddAsync(meal);

                                await ctx.SaveChangesAsync();

                                createdMealId = ctx.Meal.ToList().Last().Id;
                            }

                            await ctx.DietMeals.AddAsync(new DietMeal
                            {
                                MealId = createdMealId,
                                DietId = createdDietId
                            });

                            await ctx.SaveChangesAsync();
                        }
                    }

                    return(createdDietId);
                }
                catch (Exception e)
                {
                    return(-1);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Method for seeding the user's posts
        /// </summary>
        /// <param name="seededUserIds">the seeded users ids</param>
        private static async Task <int[]> SeedPosts(int[] seededUserIds)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                //posts with images: 2, 4, 5, 7, 8, 11, 12, 15, 16, 17, 20, 21, 23, 24
                List <int> postIdIndexesWithImages = new List <int> {
                    2, 4, 5, 7, 8, 11, 12, 15, 16, 17, 20, 21, 23, 24
                };
                int imageOwnerId = 0;
                int postOwnerId  = 0;
                for (int i = 0; i < 25; i++)
                {
                    if (postIdIndexesWithImages.Contains(i))
                    {
                        imageOwnerId = imageOwnerId % 10;
                        await ctx.Posts.AddAsync(new Post
                        {
                            Title     = GetRandomString() + " with image " + i,
                            Content   = "This is an exciting fitness post with image",
                            HasImage  = true,
                            Owner     = ctx.Users.First(u => u.Id == seededUserIds[imageOwnerId]),
                            TimeStamp = GetRandomDateTime(DateTime.Today.AddDays(-10))
                        });

                        imageOwnerId++;
                    }
                    else
                    {
                        postOwnerId = postOwnerId % 10;
                        await ctx.Posts.AddAsync(new Post
                        {
                            Title     = GetRandomString() + " " + i,
                            Content   = "This is an interesting fitness post",
                            HasImage  = false,
                            Owner     = ctx.Users.First(u => u.Id == seededUserIds[postOwnerId]),
                            TimeStamp = GetRandomDateTime(DateTime.Today.AddDays(-10))
                        });

                        postOwnerId++;
                    }

                    await ctx.SaveChangesAsync();
                }

                await ctx.SaveChangesAsync();

                var list = ctx.Posts.OrderByDescending(p => p.Id).Select(p => p.Id).Take(25).ToList();
                foreach (var i in list)
                {
                    Console.WriteLine("Added post with id " + i);
                }

                list.Reverse();
                return(list.ToArray());
            }
        }
Exemplo n.º 8
0
        public async Task <bool> EditExerciseInTrainingAsync(ExerciseWithTraining exercise)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                int      exerciseFrequency = GetNumberOfTrainingsForExercise(exercise.Id);
                Exercise exerciseDb        = await ctx.Exercise.FirstAsync(e => e.Id == exercise.Id);

                if (exerciseFrequency > 1)
                {
                    await ctx.Exercise.AddAsync(new Exercise
                    {
                        Description = exerciseDb.Description,
                        Title       = exerciseDb.Title
                    });

                    await ctx.SaveChangesAsync();

                    int exerciseId = ctx.Exercise.ToList().Last().Id;
                    Console.WriteLine("New exercise id is " + exerciseId);
                    var trainingExercises = ctx.TrainingExercises.Where(te => te.ExerciseId == exercise.Id &&
                                                                        te.TrainingId != exercise.TrainingId).ToList();
                    Console.WriteLine("Updating " + trainingExercises.Count + " training exercises");
                    foreach (var trainingExercise in trainingExercises)
                    {
                        ctx.TrainingExercises.Remove(trainingExercise);
                        await ctx.TrainingExercises.AddAsync(new TrainingExercise
                        {
                            ExerciseId = exerciseId,
                            TrainingId = trainingExercise.TrainingId
                        });
                    }
                }

                if (!string.IsNullOrEmpty(exercise.Title))
                {
                    exerciseDb.Title = exercise.Title;
                }
                if (!string.IsNullOrEmpty(exercise.Description))
                {
                    exerciseDb.Description = exercise.Description;
                }

                try
                {
                    ctx.Exercise.Update(exerciseDb);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e) {
                    return(false);
                }

                return(true);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Method for seeding the user's trainings
        /// </summary>
        /// <param name="seededUserIds">the seeded users ids</param>
        private static async Task SeedTrainings(int[] seededUserIds)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                Console.WriteLine("Seeding trainings");
                Random   random        = new Random();
                string[] trainingTypes = { "Muscle Gain", "Weight loss", "Endurance", "Strength" };
                foreach (var userId in seededUserIds)
                {
                    int userNumberOfTrainings = random.Next(3) + 1;
                    for (int i = 0; i < userNumberOfTrainings; i++)
                    {
                        await ctx.Training.AddAsync(new Training
                        {
                            Duration    = random.Next(60) + 40,
                            IsPublic    = i == 1,
                            IsCompleted = random.Next(2) == 0,
                            TimeStamp   = GetRandomTrainingTime(DateTime.Today.AddDays(-5), i),
                            Title       = GetRandomString(),
                            Owner       = ctx.Users.First(u => u.Id == userId),
                            Type        = trainingTypes[random.Next(trainingTypes.Length)]
                        });

                        await ctx.SaveChangesAsync();

                        int createdTrainingId = ctx.Training.ToList().Last().Id;

                        int trainingNumberOfExercises = random.Next(3);
                        for (int j = 0; j < trainingNumberOfExercises; j++)
                        {
                            await ctx.Exercise.AddAsync(new Exercise
                            {
                                Title       = GetRandomString(),
                                Description = "Exercise description: " + GetRandomString()
                            });

                            await ctx.SaveChangesAsync();

                            int createdExerciseId = ctx.Exercise.ToList().Last().Id;
                            await ctx.TrainingExercises.AddAsync(new TrainingExercise
                            {
                                TrainingId = createdTrainingId,
                                ExerciseId = createdExerciseId
                            });

                            await ctx.SaveChangesAsync();
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        public async Task <int> AddUserAsync(UserSocketsModel user)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    await ctx.Users.AddAsync(new User
                    {
                        Address     = user.Address,
                        City        = user.City,
                        Description = user.Description,
                        Email       = user.Email,
                        Name        = user.Name,
                        Password    = user.Password
                    });

                    await ctx.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    return(-1);
                }

                int userId = ctx.Users.First(u => u.Email.Equals(user.Email)).Id;
                Console.WriteLine("Added user to database with id " + userId);
                return(userId);
            }
        }
Exemplo n.º 11
0
        public async Task <int> AddPostAsync(PostShortVersion postShortVersion)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                User owner = await ctx.Users.FirstAsync(u => u.Id == postShortVersion.Owner.UserId);

                Post post = new Post
                {
                    Title     = postShortVersion.Title,
                    Content   = postShortVersion.Content,
                    Owner     = owner,
                    TimeStamp = DateTime.Now,
                    HasImage  = postShortVersion.HasImage
                };
                Console.WriteLine("adding post to db");
                await ctx.Posts.AddAsync(post);

                await ctx.SaveChangesAsync();

                Console.WriteLine("post saved");
                var newPost = await ctx.Posts.ToListAsync();

                return(newPost.Last().Id);
            }
        }
Exemplo n.º 12
0
        public async Task <bool> EditPostAsync(PostShortVersion post)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                Post postDb = await ctx.Posts.FirstAsync(p => p.Id == post.Id);

                if (post.Title != null)
                {
                    postDb.Title = post.Title;
                }
                if (post.Content != null)
                {
                    postDb.Content = post.Content;
                }
                try {
                    ctx.Posts.Update(postDb);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e) {
                    return(false);
                }

                return(true);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Method for seeding the user's user chat messages
        /// </summary>
        /// <param name="seededUserIds">the seeded users ids</param>
        private static async Task SeedChat(int[] seededUserIds)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                for (int i = 0; i < seededUserIds.Length - 1; i++)
                {
                    for (int j = i + 1; j < seededUserIds.Length; j++)
                    {
                        await ctx.Messages.AddAsync(new Message
                        {
                            SenderId   = seededUserIds[i],
                            ReceiverId = seededUserIds[j],
                            Content    = GetRandomString(),
                            HasImage   = false,
                            TimeStamp  = GetRandomDateTime(DateTime.Today.AddDays(-10))
                        });

                        await ctx.Messages.AddAsync(new Message
                        {
                            SenderId   = seededUserIds[j],
                            ReceiverId = seededUserIds[i],
                            Content    = GetRandomString(),
                            HasImage   = false,
                            TimeStamp  = GetRandomDateTime(DateTime.Today.AddDays(-10))
                        });
                    }
                }

                await ctx.SaveChangesAsync();
            }
        }
Exemplo n.º 14
0
        public async Task <bool> EditDietAsync(DietSocketsModel diet)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                Diet dietDb = await ctx.Diet.FirstAsync(d => d.Id == diet.Id);

                if (!string.IsNullOrEmpty(diet.Title))
                {
                    dietDb.Title = diet.Title;
                }
                if (!string.IsNullOrEmpty(diet.Description))
                {
                    dietDb.Description = diet.Description;
                }

                dietDb.IsPublic = diet.Global;

                try
                {
                    ctx.Diet.Update(dietDb);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    return(false);
                }

                return(true);
            }
        }
Exemplo n.º 15
0
        public async Task <bool> DeleteDietAsync(int dietId)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    Diet diet = await ctx.Diet.FirstAsync(d => d.Id == dietId);

                    List <Meal> mealsForDiet = ctx.DietMeals.Where(dm => dm.DietId == dietId)
                                               .Select(dm => dm.Meal).ToList();
                    foreach (var meal in mealsForDiet)
                    {
                        if (GetNumberOfDietsForMeal(meal.Id) == 1)
                        {
                            ctx.Meal.Remove(meal);
                        }
                    }

                    ctx.Diet.Remove(diet);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    return(false);
                }

                return(true);
            }
        }
Exemplo n.º 16
0
        public async Task <bool> DeleteTrainingAsync(int trainingId)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try {
                    Training training = await ctx.Training.FirstAsync(t => t.Id == trainingId);

                    List <Exercise> exercisesForTraining = ctx.TrainingExercises.Where(te => te.TrainingId == trainingId)
                                                           .Select(te => te.Exercise).ToList();
                    Console.WriteLine("Training " + trainingId + " has " + exercisesForTraining.Count + " exercises");
                    foreach (var exercise in exercisesForTraining)
                    {
                        if (GetNumberOfTrainingsForExercise(exercise.Id) == 1)
                        {
                            ctx.Exercise.Remove(exercise);
                        }
                    }
                    ctx.Training.Remove(training);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e) {
                    return(false);
                }

                return(true);
            }
        }
Exemplo n.º 17
0
        public async Task <int> PostPageRatingAsync(ModelActionSockets modelActionSockets)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                PageRating pageRating = await ctx.PageRatings.FirstOrDefaultAsync(pr =>
                                                                                  pr.UserId == modelActionSockets.SenderId && pr.PageId == modelActionSockets.ReceiverId);

                if (pageRating == null)
                {
                    await ctx.PageRatings.AddAsync(new PageRating
                    {
                        UserId = modelActionSockets.SenderId,
                        PageId = modelActionSockets.ReceiverId,
                        Rating = int.Parse(modelActionSockets.Value.ToString())
                    });
                }
                else
                {
                    pageRating.Rating = int.Parse(modelActionSockets.Value.ToString());
                    ctx.PageRatings.Update(pageRating);
                }

                await ctx.SaveChangesAsync();

                return(0);
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Method for seeding the database users
        /// </summary>
        /// <returns>the ids of the seeded users</returns>
        private static async Task <int[]> SeedUsers()
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                for (int i = 0; i < 10; i++)
                {
                    User user = new User
                    {
                        City        = "Horsens",
                        Description = "Description for user " + i,
                        Email       = "user" + i + "@email.com",
                        Name        = "Boring User" + i,
                        Password    = "******",
                        Score       = i * 10 % 40
                    };
                    if (i % 3 == 0 && i != 0) //gyms at indexes 3, 6, 9
                    {
                        Address address = new Address
                        {
                            Street = "Gym street",
                            Number = i.ToString(),
                        };
                        user.Address = address;
                    }

                    await ctx.Users.AddAsync(user);

                    await ctx.SaveChangesAsync();
                }

                await ctx.Administrators.AddAsync(new Administrator
                {
                    Email    = "*****@*****.**",
                    Password = "******"
                });

                await ctx.SaveChangesAsync();

                var list = ctx.Users.OrderByDescending(u => u.Id).Select(u => u.Id).Take(10).ToList();
                list.Reverse();
                foreach (var i in list)
                {
                    Console.WriteLine("Added user with id " + i);
                }
                return(list.ToArray());
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Method for seeding the user's diets
        /// </summary>
        /// <param name="seededUserIds">the seeded users ids</param>
        private static async Task SeedDiets(int[] seededUserIds)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                Random random = new Random();
                foreach (var userId in seededUserIds)
                {
                    int userNumberOfDiets = random.Next(3) + 1;
                    for (int i = 0; i < userNumberOfDiets; i++)
                    {
                        await ctx.Diet.AddAsync(new Diet
                        {
                            IsPublic    = i == 1,
                            Description = "Diet description: " + GetRandomString(),
                            Title       = GetRandomString(),
                            Owner       = ctx.Users.First(u => u.Id == userId),
                        });

                        await ctx.SaveChangesAsync();

                        int createdDietId = ctx.Diet.ToList().Last().Id;

                        int dietNumberOfMeals = random.Next(3);
                        for (int j = 0; j < dietNumberOfMeals; j++)
                        {
                            await ctx.Meal.AddAsync(new Meal
                            {
                                Title       = GetRandomString(),
                                Description = "Meal description: " + GetRandomString(),
                                Calories    = (random.Next(12) + 1) * 100
                            });

                            await ctx.SaveChangesAsync();

                            int createdMealId = ctx.Meal.ToList().Last().Id;
                            await ctx.DietMeals.AddAsync(new DietMeal
                            {
                                DietId = createdDietId,
                                MealId = createdMealId
                            });

                            await ctx.SaveChangesAsync();
                        }
                    }
                }
            }
        }
Exemplo n.º 20
0
        public async Task <bool> EditUserAsync(UserSocketsModel user)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                User userDb = await ctx.Users.FirstAsync(u => u.Id == user.Id);

                if (user.Email != null)
                {
                    userDb.Email = user.Email;
                }
                else //this means that the request is only supposed to edit the bg picture, without making changes to the db
                {
                    return(true);
                }

                if (user.City != null)
                {
                    userDb.City = user.City;
                }
                if (user.Description != null)
                {
                    userDb.Description = user.Description;
                }
                if (user.Name != null)
                {
                    userDb.Name = user.Name;
                }
                if (user.Score != 0)
                {
                    userDb.Score = user.Score;
                }
                if (user.Address != null)
                {
                    userDb.Address = user.Address;
                }
                if (user.Password != null)
                {
                    userDb.Password = user.Password;
                }

                try {
                    ctx.Users.Update(userDb);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e) {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 21
0
        private async Task <int> AddNotification(string notificationType, int senderId, int receiverId)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                await ctx.Notifications.AddAsync(new Notification {
                    SenderId         = senderId,
                    ReceiverId       = receiverId,
                    NotificationType = notificationType
                });

                await ctx.SaveChangesAsync();

                return(ctx.Notifications.First(n => n.SenderId == senderId && n.ReceiverId == receiverId &&
                                               n.NotificationType.Equals(notificationType)).Id);
            }
        }
Exemplo n.º 22
0
        public async Task <bool> DeleteCommentFromPost(int commentId)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                Comment comment = await ctx.Comment.FirstAsync(c => c.Id == commentId);

                try {
                    ctx.Comment.Remove(comment);
                    await ctx.SaveChangesAsync();

                    return(true);
                }
                catch (Exception e) {
                    return(false);
                }
            }
        }
Exemplo n.º 23
0
        public async Task <bool> DeletePostAsync(int postId)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try {
                    Post post = await ctx.Posts.Where(p => p.Id == postId)
                                .Include(p => p.Comments).FirstAsync();

                    ctx.Posts.Remove(post);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e) {
                    return(false);
                }

                return(true);
            }
        }
Exemplo n.º 24
0
        public async Task <bool> DeleteNotificationAsync(int notificationId)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    Notification notification = await ctx.Notifications.FirstAsync(n => n.Id == notificationId);

                    ctx.Notifications.Remove(notification);
                    await ctx.SaveChangesAsync();

                    return(true);
                }
                catch (Exception e)
                {
                    return(false);
                }
            }
        }
Exemplo n.º 25
0
        public async Task <bool> IncrementUserScoreAsync(int userId, int amount)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    User user = await ctx.Users.FirstAsync(u => u.Id == userId);

                    user.Score += amount;
                    ctx.Users.Update(user);
                    await ctx.SaveChangesAsync();

                    return(true);
                }
                catch (Exception e) {
                    return(false);
                }
            }
        }
Exemplo n.º 26
0
        public async Task <bool> DeleteMessageAsync(int messageId)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    Message message = await ctx.Messages.FirstAsync(m => m.Id == messageId);

                    message.Content  = null;
                    message.HasImage = false;
                    ctx.Messages.Update(message);
                    await ctx.SaveChangesAsync();

                    return(true);
                }
                catch (Exception e)
                {
                    return(false);
                }
            }
        }
Exemplo n.º 27
0
        public async Task <bool> EditTrainingAsync(TrainingSocketsModel training)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                Training trainingDb = await ctx.Training.FirstAsync(t => t.Id == training.Id);

                if (!string.IsNullOrEmpty(training.Title))
                {
                    trainingDb.Title = training.Title;
                }
                if (!string.IsNullOrEmpty(training.Type))
                {
                    trainingDb.Type = training.Type;
                }
                if (training.Duration > 0)
                {
                    trainingDb.Duration = training.Duration;
                }
                if (training.TimeStamp.Year != 1)
                {
                    trainingDb.TimeStamp = training.TimeStamp;
                }

                trainingDb.IsCompleted = training.Completed;
                trainingDb.IsPublic    = training.Global;

                try
                {
                    ctx.Training.Update(trainingDb);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e) {
                    return(false);
                }

                return(true);
            }
        }
Exemplo n.º 28
0
        public async Task <int> RemoveFriendshipAsync(ModelActionSockets modelActionSockets)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    Friendship friendship = await ctx.Friendships.FirstAsync(fr =>
                                                                             (fr.FirstUserId == modelActionSockets.SenderId &&
                                                                              fr.SecondUserId == modelActionSockets.ReceiverId) ||
                                                                             (fr.FirstUserId == modelActionSockets.ReceiverId &&
                                                                              fr.SecondUserId == modelActionSockets.SenderId));

                    ctx.Friendships.Remove(friendship);
                    await ctx.SaveChangesAsync();

                    return(0);
                }
                catch (Exception e)
                {
                    return(-1);
                }
            }
        }
Exemplo n.º 29
0
        public async Task <int> AddCommentToPost(CommentForPost comment)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                Post post = await ctx.Posts.Where(p => p.Id == comment.PostId)
                            .Include(p => p.Comments).FirstAsync();

                User owner = await ctx.Users.FirstAsync(u => u.Id == comment.OwnerId);

                Comment commentDb = new Comment
                {
                    Content   = comment.Content,
                    TimeStamp = DateTime.Now,
                    Owner     = owner
                };
                post.Comments.Add(commentDb);
                ctx.Posts.Update(post);
                await ctx.Comment.AddAsync(commentDb);

                await ctx.SaveChangesAsync();

                return(ctx.Comment.Max(c => c.Id));
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Method for seeding the seeded user's interaction with the seeded posts
        /// </summary>
        /// <param name="seededUserIds">the seeded users ids</param>
        /// <param name="seededPostIds">the seeded posts ids</param>
        private static async Task SeedPostInteractions(int[] seededPostIds, int[] seededUserIds)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                Console.WriteLine("Seeding post interactions");
                Random random = new Random();
                //post actions
                foreach (var postId in seededPostIds)
                {
                    int numberOfLikes   = random.Next(5) + 5;                       //between 5 and 10, without 10
                    int numberOfReports = seededUserIds.Length - numberOfLikes - 1; //between 0 and 4

                    for (int i = 4; i < numberOfLikes; i++)
                    {
                        int userIdForLike = i;
                        await ctx.PostActions.AddAsync(new PostAction
                        {
                            PostId = postId,
                            IsLike = true,
                            UserId = seededUserIds[userIdForLike]
                        });
                    }

                    for (int i = 0; i < numberOfReports; i++)
                    {
                        int userIdForReport = i;

                        await ctx.PostActions.AddAsync(new PostAction
                        {
                            PostId   = postId,
                            IsReport = true,
                            UserId   = seededUserIds[userIdForReport]
                        });
                    }
                }

                await ctx.SaveChangesAsync();

                //comments
                foreach (var postId in seededPostIds)
                {
                    Post post = await ctx.Posts.Where(p => p.Id == postId)
                                .Include(p => p.Comments).FirstAsync();

                    int numberOfComments = random.Next(5) + 1;
                    for (int i = 0; i < numberOfComments; i++)
                    {
                        int     userIdIndexForComment = i * 3 % seededUserIds.Length;;
                        Comment comment = new Comment
                        {
                            Content   = GetRandomString(),
                            Owner     = ctx.Users.First(u => u.Id == seededUserIds[userIdIndexForComment]),
                            TimeStamp = post.TimeStamp.AddMinutes(random.Next(20) + 1)
                        };
                        post.Comments.Add(comment);
                        await ctx.Comment.AddAsync(comment);

                        ctx.Posts.Update(post);
                    }
                    await ctx.SaveChangesAsync();
                }

                await ctx.SaveChangesAsync();
            }
        }