Exemplo n.º 1
0
        public async Task <bool> ArrangeMeeting(Meeting meeting, int schoolId, int userId, int currentUserId)
        {
            if (meeting == null)
            {
                throw new EntityNotFoundException("Spotkanie");
            }

            var school = await database.UserRepository.Get <School>(schoolId);

            var user = await database.UserRepository.Get <User>(userId);

            if (currentUserId != userId && currentUserId != school.OwnerId)
            {
                return(false);
            }

            if (user.Meetings.Where(m => m.SchoolId == schoolId).Count() > 5)
            {
                return(false);
            }

            meeting.SetSchoolAndUserId(school.Id, user.Id);
            meeting.SetArrangedBy(currentUserId);

            school.Meetings.Add(meeting);
            user.Meetings.Add(meeting);

            if (!await database.Complete())
            {
                return(false);
            }

            if (currentUserId == school.OwnerId)
            {
                await notificationSystem.PushNotification(user.Id, StaticExpressions.NewMeetingArrangedByOwner(school.Name));
            }
            else
            {
                await notificationSystem.PushNotification(school.OwnerId, StaticExpressions.NewMeetingArranged(user.UserName));
            }

            return(true);
        }