public async Task <IActionResult> AddEventResponse(string userId, string eventId, bool isGoing)
        {
            User user = await userManager.FindByIdAsync(userId);

            Event myEvent = eventRepository.GetEvent(eventId);

            if (user != null && myEvent != null)
            {
                IsGoing response = new IsGoing
                {
                    Event    = myEvent,
                    EventId  = myEvent.Id,
                    User     = user,
                    UserId   = user.Id,
                    Response = isGoing
                };
                var searchIsGoing = isGoingRepository.FindRecord(userId, eventId);
                if (searchIsGoing != null)
                {
                    isGoingRepository.Delete(searchIsGoing);
                }

                isGoingRepository.Create(response);
                return(Ok());
            }

            return(BadRequest("user or event not found."));
        }
        public async Task <IActionResult> Leave(string userId, string groupId)
        {
            Group group = groupRepository.GetGroup(groupId);
            User  user  = await userManager.FindByIdAsync(userId);

            if (user != null && group != null)
            {
                if (groupUserRepository.DeleteUser(userId, groupId) != null)
                {
                    //user deleted, from group, now cleaning up events and responses
                    var groupEvents = eventRepository.GetGroupEvents(group.GroupId);
                    foreach (Event groupEvent in groupEvents)
                    {
                        IsGoing newUserResponse = new IsGoing
                        {
                            User    = user,
                            UserId  = userId,
                            Event   = groupEvent,
                            EventId = groupEvent.Id
                        };
                        isGoingRepository.Delete(newUserResponse);
                    }
                    return(Ok());
                }
            }
            return(BadRequest("User or Group not found."));
        }
        public async Task <IActionResult> CreateEvent(string userId, string groupId, [FromForm] EventEntity newEvent)
        {
            User user = await userManager.FindByIdAsync(userId);

            Group group = groupRepository.GetGroup(groupId);

            if (user != null && group != null)
            {
                Event createdEvent = new Event
                {
                    Id           = Guid.NewGuid().ToString(),
                    GroupId      = groupId,
                    Group        = group,
                    Name         = newEvent.Name,
                    Description  = newEvent.Description,
                    StartTime    = newEvent.StartTime,
                    EndTime      = newEvent.EndTime,
                    Localization = newEvent.Localization
                };
                eventRepository.Add(createdEvent);
                var groupUsers = groupUserRepository.GetGroupUsers(groupId);
                foreach (GroupUser groupUser in groupUsers)
                {
                    User currentUser = await userManager.FindByIdAsync(groupUser.UserId);

                    IsGoing isGoing;
                    if (currentUser.Id == userId)
                    {
                        isGoing = new IsGoing
                        {
                            UserId   = currentUser.Id,
                            User     = currentUser,
                            Event    = createdEvent,
                            EventId  = createdEvent.Id,
                            Response = true
                        };
                    }
                    else
                    {
                        isGoing = new IsGoing
                        {
                            UserId  = currentUser.Id,
                            User    = currentUser,
                            Event   = createdEvent,
                            EventId = createdEvent.Id
                        };
                    }
                    isGoingRepository.Create(isGoing);
                }
                return(Ok());
            }
            return(BadRequest("Group or User not found."));
        }
        public async Task <IActionResult> Join(string userId, string invId)
        {
            Group group = groupRepository.GetGropByInvLink(invId);
            User  user  = await userManager.FindByIdAsync(userId);

            if (user != null && group != null)
            {
                GroupUser groupUser = new GroupUser
                {
                    User    = user,
                    UserId  = user.Id,
                    Group   = group,
                    GroupId = group.GroupId
                };
                try
                {
                    groupUserRepository.Add(groupUser);
                }
                catch (Exception e)
                {
                    return(BadRequest("User already present in the group."));
                }

                //user added to group, now he needs to be present in all events and event responses

                var groupEvents = eventRepository.GetGroupEvents(group.GroupId);
                foreach (Event groupEvent in groupEvents)
                {
                    IsGoing newUserResponse = new IsGoing
                    {
                        User    = user,
                        UserId  = userId,
                        Event   = groupEvent,
                        EventId = groupEvent.Id
                    };
                    isGoingRepository.Create(newUserResponse);
                }

                return(Ok());
            }

            return(BadRequest("Wrong User or idInv."));
        }
Beispiel #5
0
 public IsGoingEntity(IsGoing isGoing)
 {
     UserId   = isGoing.UserId;
     UserName = isGoing.User.UserName;
     IsGoing  = isGoing.Response;
 }