Exemple #1
0
        public async Task <bool> AddFriendAsync(long userId, long friendId)
        {
            if (userId == friendId)
            {
                throw new ArgumentException("Can't be the same id");
            }

            var users = await _dbContext.Users.Where(x => x.Id == userId || x.Id == friendId).ToListAsync();

            if (users.Count != 2)
            {
                throw new ArgumentException("No such user");
            }

            var existingFriend = await _dbContext.Friends.FirstOrDefaultAsync(x =>
                                                                              x.UserId == userId && x.FriendId == friendId ||
                                                                              x.FriendId == userId && x.UserId == friendId);

            if (existingFriend != null && existingFriend.FriendStatus == FriendStatus.Friend)
            {
                return(false);
            }
            else if (existingFriend != null && existingFriend.FriendStatus == FriendStatus.Deleted)
            {
                existingFriend.FriendStatus = FriendStatus.Friend;

                await _dbContext.SaveChangesAsync();

                return(true);
            }
            else if (existingFriend == null)
            {
                _dbContext.Friends.Add(new Friend
                {
                    UserId              = userId,
                    FriendId            = friendId,
                    FriendStatus        = FriendStatus.Friend,
                    FriendshipStartDate = DateTime.Now
                });

                await _dbContext.SaveChangesAsync();

                return(true);
            }
            return(false);
        }
        public async Task CompleteUserGoalAsync(long userId, long goalId, bool isCompleted)
        {
            var checkUserId = await _dbContext.Users.AnyAsync(x => x.Id == userId);

            if (!checkUserId)
            {
                throw new ArgumentException("No such user");
            }

            var checkGoal = await _dbContext.UserGoals
                            .FirstOrDefaultAsync(x => x.User.Id == userId && x.Goal.Id == goalId && x.IsCompleted == false);

            if (checkGoal == null)
            {
                throw new ArgumentException("No such goal");
            }
            else
            {
                checkGoal.IsCompleted = true;
                checkGoal.FinishDate  = DateTime.Now;
                await _dbContext.SaveChangesAsync();
            }
        }
Exemple #3
0
        public async Task <bool> TakePartInGlobalGoalsAsync(long userId, long goalId)
        {
            var checkUserId = await _dbContext.Users.AnyAsync(x => x.Id == userId);

            var checGoalId = await _dbContext.Goals.AnyAsync(x => x.Id == goalId && x.GoalType == GoalType.Global);

            if (!checkUserId)
            {
                throw new ArgumentException("No such user");
            }

            if (!checGoalId)
            {
                throw new ArgumentException("No such global goal");
            }

            var exist = await _dbContext.UserGoals
                        .FirstOrDefaultAsync(x => x.UserId == userId && x.GoalId == goalId && x.Goal.GoalType == GoalType.Global);

            if (exist != null)
            {
                return(false);
            }
            else
            {
                _dbContext.UserGoals.Add(new UserGoal
                {
                    UserId      = userId,
                    GoalId      = goalId,
                    IsCompleted = false,
                    StartDate   = DateTime.Now,
                    IsDeleted   = false,
                });

                await _dbContext.SaveChangesAsync();

                return(true);
            }
        }
        public async Task <bool> DownloadUserProfilePictureAsync(long userId, byte[] picture, string contentType)
        {
            var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                throw new ArgumentException("No such user");
            }
            else if (!mimeTypes.Contains(contentType))
            {
                return(false);
            }
            else
            {
                user.ContentType    = contentType;
                user.ProfilePicture = picture;

                await _dbContext.SaveChangesAsync();

                return(true);
            }
        }
        public async Task <bool> AddUserAsync(string email, string password, string firstName, string lastName)
        {
            var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.Email == email.ToLower());

            if (user == null)
            {
                _dbContext.Users
                .Add(
                    new User
                {
                    Email        = email.ToLower(),
                    PasswordHash = ComputeSHA512(password),
                    Role         = Role.User,
                    FirstName    = firstName,
                    LastName     = lastName
                });
                await _dbContext.SaveChangesAsync();

                return(true);
            }

            return(false);
        }