Exemple #1
0
        public async Task <IActionResult> OnPostSendJoinRequestAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");

            var group = await _context.Group
                        .OfType <MultiuserGroup>()
                        .Include(g => g.BannedUsers)
                        .FirstOrDefaultAsync(g => g.GroupID == SendJoinRequestInput.GroupID);

            if (group.BannedUsers.FirstOrDefault(u => u.Id == SendJoinRequestInput.ChatUserID) != null)
            {
                return(BadRequest());
            }

            GroupJoinRequest request = new GroupJoinRequest()
            {
                ChatUserID = SendJoinRequestInput.ChatUserID,
                GroupID    = group.GroupID,
                Message    = SendJoinRequestInput.Message,
                DateSent   = DateTime.Now
            };

            await _context.GroupJoinRequest.AddAsync(request);

            await _context.SaveChangesAsync();

            return(LocalRedirect(returnUrl));
        }
Exemple #2
0
        public async Task <BaseResponse> Join([FromBody] GroupJoinRequest request)
        {
            var response = new BaseResponse();

            if (request == null || !ModelState.IsValid)
            {
                HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
                response.Message = "Invalid input(s).";
                return(response);
            }

            try
            {
                var targetGroup = await _groupsRepository.GetById(request.Group_Id);

                var usersInGroup = await _groupsRepository.GetUsersInGroup(request.Group_Id);

                if (usersInGroup.Count() < targetGroup.Size)
                {
                    if (targetGroup.Private)
                    {
                        // TODO: handle checking for invite
                    }

                    else
                    {
                        if (await _groupsRepository.AddUser(HttpContext.User.GetUserId(), request.Group_Id))
                        {
                            response.Message = "Successfully joined group!";
                            response.Success = true;
                        }
                    }
                }

                else
                {
                    response.Message = "Join unsuccessful.  Group is full.";
                    response.Success = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
                response.Message = "Failed to join group.";
            }

            return(response);
        }