public async Task <User> Register(User user, string password)
        {
            user.CreatePasswordHash(password);

            await companyRepository.Get(user.CompanyId);

            await userRepository.Insert(user);

            Company company = await companyRepository.Get(1);

            user.Company = company;
            company.Users.Add(user);

            await companyRepository.Update(company);

            await userRepository.Update(user);

            Discussion general = await this.discussionRepository.GetDiscussionByName("general");

            UserInDiscussion userInGeneral = new UserInDiscussion {
                JoinedAt     = DateTime.Now,
                DiscussionId = general.Id
            };

            await userInDiscussionRepository.Insert(userInGeneral);

            return(user);
        }
Esempio n. 2
0
        public async Task <ObjectResult> Handle(CreateGroupRequest request)
        {
            string name          = request.createGroupDTO.Name;
            int    currentUserId = int.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value);


            Group group = new Group
            {
                Name         = name,
                CreatedAt    = DateTime.Now,
                SupervisorId = null
            };

            await _appDBContext.Groups.AddAsync(group);

            await _appDBContext.SaveChangesAsync();

            ParticipantGroup participantGroups = new ParticipantGroup
            {
                IsCreator        = true,
                Status           = Status.Accepted,
                BelongingGroupId = group.Id,
                UserId           = currentUserId
            };

            await _appDBContext.ParticipantGroups.AddAsync(participantGroups);

            await _appDBContext.SaveChangesAsync();

            Discussion discussion = new Discussion
            {
                CreatedAt = DateTime.Now,
                Name      = name,
                GroupId   = group.Id
            };

            await _appDBContext.Discussions.AddAsync(discussion);

            await _appDBContext.SaveChangesAsync();

            UserInDiscussion userInDiscussion = new UserInDiscussion
            {
                JoinedAt      = DateTime.Now,
                DiscussionId  = discussion.Id,
                ParticipantId = currentUserId
            };

            await _appDBContext.UsersInDiscussion.AddAsync(userInDiscussion);

            await _appDBContext.SaveChangesAsync();

            List <ParticipantGroup> requestsToDelete = await _appDBContext.ParticipantGroups
                                                       .Where(pg => pg.Status == Status.Waiting)
                                                       .ToListAsync();

            _appDBContext.ParticipantGroups.RemoveRange(requestsToDelete);
            await _appDBContext.SaveChangesAsync();

            return(new OkObjectResult(true));
        }
Esempio n. 3
0
        public async Task RemoveFromDiscussion(int userId, int threadId)
        {
            Discussion discussion = await this.discussionRepository.GetDiscussionWithUID(threadId);

            UserInDiscussion userTodelete = discussion.UsersInDiscussion.Where(uid => uid.ParticipantId == userId).FirstOrDefault();

            await this.userIniscussionRepository.Delete(userTodelete);
        }
Esempio n. 4
0
        public async Task AddInDiscussion(int userId, int threadId)
        {
            UserInDiscussion userInDiscussion = new UserInDiscussion {
                JoinedAt      = DateTime.Now,
                DiscussionId  = threadId,
                ParticipantId = userId
            };

            await this.userIniscussionRepository.Insert(userInDiscussion);
        }
        public async Task <ObjectResult> Handle(AcceptGroupRequestRequest request)
        {
            ParticipantGroup membershipToUpdate = await _appDBContext.ParticipantGroups
                                                  .Where(pg => pg.Id == request.Id && pg.Status == Status.Waiting)
                                                  .FirstOrDefaultAsync();

            if (membershipToUpdate == null)
            {
                return(new NotFoundObjectResult("No corresponding request was found"));
            }

            int maxGroupSize = await _appDBContext.Companies
                               .Where(c => c.Id == 1)
                               .Select(c => c.MaxGroupSize)
                               .FirstOrDefaultAsync();

            int actualGroupSize = await _appDBContext.Groups
                                  .Where(g => g.Id == membershipToUpdate.BelongingGroupId)
                                  .Select(g => g.Members.Where(pg => pg.Status == Status.Accepted).Count())
                                  .FirstOrDefaultAsync();

            if (actualGroupSize < maxGroupSize)
            {
                membershipToUpdate.Status = Status.Accepted;

                await _appDBContext.SaveChangesAsync();

                List <ParticipantGroup> requestsToDelete = await _appDBContext.ParticipantGroups
                                                           .Where(pg => pg.UserId == membershipToUpdate.UserId && pg.Status == Status.Waiting)
                                                           .ToListAsync();

                _appDBContext.ParticipantGroups.RemoveRange(requestsToDelete);
                await _appDBContext.SaveChangesAsync();

                UserInDiscussion userInDiscussion = new UserInDiscussion
                {
                    JoinedAt      = DateTime.Now,
                    DiscussionId  = membershipToUpdate.BelongingGroupId,
                    ParticipantId = membershipToUpdate.UserId
                };

                await _appDBContext.UsersInDiscussion.AddAsync(userInDiscussion);

                await _appDBContext.SaveChangesAsync();

                return(new OkObjectResult(true));
            }
            else
            {
                return(new BadRequestObjectResult("Group has already ready reach max size"));
            }
        }
Esempio n. 6
0
        public async Task <ObjectResult> Handle(ChangeRoleRequest request)
        {
            User user = await _appDBContext.Users
                        .Where(u => u.Id == request.Id)
                        .Include(u => u.GroupsSupervised)
                        .ThenInclude(g => g.Discussion)
                        .Include(u => u.UsersInDiscussion)
                        .ThenInclude(uid => uid.Discussion)
                        .FirstOrDefaultAsync();

            if (user == null)
            {
                return(new NotFoundObjectResult("No corresponding user was found"));
            }

            if ((user.Role == Role.Admin || user.Role == Role.Checker) && (Role)request.RoleId == Role.Member)
            {
                List <UserInDiscussion> usersInDiscussion = user.UsersInDiscussion.Where(uid => user.GroupsSupervised.Any(g => g.DiscussionId == uid.Discussion.Id) || uid.Discussion.Name == "staff").ToList();
                _appDBContext.UsersInDiscussion.RemoveRange(usersInDiscussion);

                foreach (var group in user.GroupsSupervised)
                {
                    group.SupervisorId = null;
                }

                await _appDBContext.SaveChangesAsync();
            }

            if (user.Role == Role.Member && (Role)request.RoleId != Role.Member)
            {
                int discussioStaffId = await _appDBContext.Discussions
                                       .Where(d => d.Name == "staff")
                                       .Select(d => d.Id)
                                       .FirstOrDefaultAsync();

                UserInDiscussion userInDiscussion = new UserInDiscussion
                {
                    JoinedAt      = DateTime.Now,
                    ParticipantId = user.Id,
                    DiscussionId  = discussioStaffId
                };

                await _appDBContext.UsersInDiscussion.AddAsync(userInDiscussion);
            }

            user.Role = (Role)request.RoleId;
            await _appDBContext.SaveChangesAsync();

            return(new OkObjectResult(true));
        }
        public async Task <ObjectResult> Handle(AttributeSupervisorRequest request)
        {
            Group groupToUpdate = await _appDBContext.Groups
                                  .Where(g => g.Id == request.Id)
                                  .FirstOrDefaultAsync();

            if (groupToUpdate == null)
            {
                return(new NotFoundObjectResult("No corresponding group was found"));
            }

            if (groupToUpdate.SupervisorId != null)
            {
                UserInDiscussion userInDiscussionToDelete = await _appDBContext.UsersInDiscussion
                                                            .Where(uid => uid.ParticipantId == groupToUpdate.SupervisorId)
                                                            .FirstOrDefaultAsync();

                _appDBContext.UsersInDiscussion.Remove(userInDiscussionToDelete);
            }

            groupToUpdate.SupervisorId = request.SupervisorId;

            await _appDBContext.SaveChangesAsync();

            UserInDiscussion userInDiscussion = new UserInDiscussion
            {
                JoinedAt      = DateTime.Now,
                DiscussionId  = groupToUpdate.Id,
                ParticipantId = request.SupervisorId
            };

            await _appDBContext.UsersInDiscussion.AddAsync(userInDiscussion);

            await _appDBContext.SaveChangesAsync();


            return(new OkObjectResult(true));
        }
Esempio n. 8
0
        public async Task <ObjectResult> Handle(SignupRequest request)
        {
            (string username, string email, string password, IFormFile profilePicture) = request;

            Account account = new Account(
                this._appSettings.GetSection("Cloudinary:CloudName").Value,
                this._appSettings.GetSection("Cloudinary:ApiKey").Value,
                this._appSettings.GetSection("Cloudinary:ApiSecret").Value);

            Cloudinary cloudinary = new Cloudinary(account);

            string urlPicture = this._appSettings.GetSection("Cloudinary:DefaultPictureProfile").Value;

            if (profilePicture != null)
            {
                Stream stream = profilePicture.OpenReadStream();

                var uploadParams = new ImageUploadParams()
                {
                    File     = new FileDescription(username, stream),
                    PublicId = this._appSettings.GetSection("Cloudinary:ProfilePicturesFolder").Value + username
                };

                var uploadResult = cloudinary.Upload(uploadParams);
                urlPicture = uploadResult.SecureUri.AbsolutePath;
            }

            User user = new User
            {
                Username  = username,
                Email     = email,
                Picture   = this._appSettings.GetSection("Cloudinary:BaseUrl").Value + urlPicture,
                Role      = Role.Member,
                CompanyId = 1
            };

            user.ParticipantGroups = null;

            user.CreatePassword(password);

            string tokenSecret = this._appSettings.GetSection("Settings:Secret").Value;

            user.CreateToken(tokenSecret);

            await _appDBContext.Users.AddAsync(user);

            await _appDBContext.SaveChangesAsync();

            int discussionGeneralId = await _appDBContext.Discussions
                                      .Where(d => d.Name == "general")
                                      .Select(d => d.Id)
                                      .FirstOrDefaultAsync();

            UserInDiscussion userInGeneral = new UserInDiscussion
            {
                JoinedAt      = DateTime.Now,
                DiscussionId  = discussionGeneralId,
                ParticipantId = user.Id
            };

            await _appDBContext.UsersInDiscussion.AddAsync(userInGeneral);

            await _appDBContext.SaveChangesAsync();

            return(new OkObjectResult(user.ToDTO()));
        }