public async Task <IActionResult> AddOffer(int schoolId, OfferForCreateDto offerForCreateDto)
        {
            var school = await database.SchoolRepository.Get <School>(schoolId);

            int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != school.OwnerId)
            {
                return(this.Forbidden("Nie jesteś właścicielem tej szkółki"));
            }

            var offer   = mapper.Map <Offer>(offerForCreateDto);
            var package = mapper.Map <Package>(offerForCreateDto.PackageForCreateDto);

            if (await schoolService.AddOffer(offer, school, package))
            {
                var offerToReturn = mapper.Map <OfferDetailsDto>(offer);

                await notificationSystem.PushNotificationToUsersByFollows(StaticExpressions.OfferCreated(school.Name, offer.Name), school);

                return(Ok(offerToReturn));
            }

            return(BadRequest("Nie udało się dodać oferty"));
        }
        public async Task <IActionResult> UpdateTrip(int schoolId, int tripId, TripForUpdateDto tripForUpdateDto)
        {
            var school = await database.SchoolRepository.Get <School>(schoolId);

            var trip = school.Trips.FirstOrDefault(t => t.Id == tripId);

            if (trip == null)
            {
                throw new EntityNotFoundException("Wycieczka");
            }

            int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != school.OwnerId)
            {
                return(this.Forbidden("Nie jesteś właścicielem tej szkółki"));
            }

            trip = mapper.Map <TripForUpdateDto, Trip>(tripForUpdateDto, trip);

            if (await database.Complete())
            {
                await notificationSystem.PushNotificationToUsersByTripFollows(StaticExpressions.TripUpdated(school.Name, trip.Name), trip);

                return(NoContent());
            }

            return(BadRequest("Nie udało się zaktualizować wycieczki"));
        }
        public async Task <IActionResult> UpdateOffer(int schoolId, int offerId, OfferForUpdateDto offerForUpdateDto)
        {
            var school = await database.SchoolRepository.Get <School>(schoolId);

            var offer = school.Offers.FirstOrDefault(o => o.Id == offerId);

            if (offer == null)
            {
                throw new EntityNotFoundException("Oferta");
            }

            int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != school.OwnerId)
            {
                return(this.Forbidden("Nie jesteś właścicielem tej szkółki"));
            }

            offer = mapper.Map <OfferForUpdateDto, Offer>(offerForUpdateDto, offer);

            if (await database.Complete())
            {
                await notificationSystem.PushNotificationToUsersByFollows(StaticExpressions.OfferUpdated(school.Name, offer.Name), school);

                return(NoContent());
            }

            return(BadRequest("Nie udało się zaktualizować ofery"));
        }
        public async Task <IActionResult> SetLogo(int schoolId, [FromForm] IFormFile logo)
        {
            if (logo != null &&
                !filesUploader.IsValidExtension(logo.FileName, PhotoExtensions))
            {
                return(BadRequest("Logo posiada niepoprawny format"));
            }

            int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

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

            if (currentUserId != school.OwnerId && !await rolesService.IsPermitted(RolesPermitted, currentUserId))
            {
                return(this.Forbidden("Nie jesteś właścicielem tej szkółki"));
            }

            if (await SetLogoUrl(logo, school))
            {
                await notificationSystem.PushNotificationToUsersByFollows(StaticExpressions.SchoolUpdated(school.Name), school);

                return(NoContent());
            }

            return(BadRequest("Logo nie zostało zaktualizowane"));
        }
        public async Task <IActionResult> AddOpinion(int schoolId, OpinionForCreateDto opinionForCreateDto)
        {
            var school = await database.SchoolRepository.Get <School>(schoolId);

            int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (schoolService.UserOpinionExists(currentUserId, school))
            {
                return(BadRequest("Dla wybranej szkółki możesz dodać tylko jedną opinię"));
            }

            if (currentUserId == school.OwnerId)
            {
                return(this.Forbidden("Nie możesz dodać opinii dla swojej szkółki"));
            }

            var opinion = mapper.Map <Opinion>(opinionForCreateDto);

            if (await schoolService.AddOpinion(opinion, school, currentUserId))
            {
                var opinionToReturn = mapper.Map <OpinionDetailsDto>(opinion);

                await notificationSystem.PushNotification(school.OwnerId, StaticExpressions.OpinionCreatedForOwner(opinion.User.UserName), NotificationType.School);

                await notificationSystem.PushNotificationToUsersByFollows(StaticExpressions.OpinionCreated(opinion.User.UserName, school.Name), school);

                return(Ok(opinionToReturn));
            }

            return(BadRequest("Nie udało się dodać opinii"));
        }
        public async Task <IActionResult> UpdateOpinion(int schoolId, int opinionId, OpinionForUpdateDto opinionForUpdateDto)
        {
            var school = await database.SchoolRepository.Get <School>(schoolId);

            var opinion = school.Opinions.FirstOrDefault(o => o.Id == opinionId);

            int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (opinion == null)
            {
                throw new EntityNotFoundException("Opinia");
            }

            if (currentUserId != opinion.UserId)
            {
                return(this.Forbidden("Nie możesz aktualizować nie swojej opinii"));
            }

            opinion = mapper.Map <OpinionForUpdateDto, Opinion>(opinionForUpdateDto, opinion);

            if (await schoolService.UpdateOpinion(school, opinion))
            {
                await notificationSystem.PushNotificationToUsersByFollows(StaticExpressions.OpinionUpdatedForOwner(opinion.User.UserName), school);

                await notificationSystem.PushNotificationToUsersByFollows(StaticExpressions.OpinionUpdated(opinion.User.UserName, school.Name), school);

                return(NoContent());
            }

            return(BadRequest("Nie udało się zaktualizować opinii"));
        }
        public async Task <IActionResult> DeleteLogo(int schoolId)
        {
            var school = await database.SchoolRepository.Get <School>(schoolId);

            if (string.IsNullOrEmpty(school.LogoUrl))
            {
                return(BadRequest("Brak logo do usunięcia"));
            }

            int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != school.OwnerId && !await rolesService.IsPermitted(RolesPermitted, currentUserId))
            {
                return(this.Forbidden("Nie jesteś właścicielem tej szkółki"));
            }

            if (await RemoveLogoUrl(school))
            {
                await notificationSystem.PushNotificationToUsersByFollows(StaticExpressions.SchoolUpdated(school.Name), school);

                return(NoContent());
            }

            return(BadRequest("Logo nie zostało usunięte"));
        }
Exemple #8
0
        public async Task <bool> DeleteTrip(int tripId, School school)
        {
            if (school == null)
            {
                throw new EntityNotFoundException("Szkółka");
            }

            var trip = school.Trips.FirstOrDefault(t => t.Id == tripId);

            if (trip == null)
            {
                throw new EntityNotFoundException("Wycieczka");
            }

            school.Trips.Remove(trip);

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

            await notificationSystem.PushNotificationToUsersByTripFollows(StaticExpressions.TripDeleted(school.Name, trip.Name), trip);

            return(true);
        }
        public async Task <bool> SendReport(Report report, ReportMessage reportMessage, User user)
        {
            if (report == null || user == null || reportMessage == null)
            {
                throw new EntityNotFoundException();
            }

            if (user.Reports.Count == 3)
            {
                return(false);
            }

            user.Reports.Add(report);
            user.ReportMessages.Add(reportMessage);
            report.ReportMessages.Add(reportMessage);

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

            await notificationSystem.PushNotificationToUsersByRoles(StaticExpressions.NewReportCreated(user.UserName), AdminRoles);

            return(true);
        }
        public async Task <IActionResult> CreateSchool([FromForm] SchoolForCreateDto schoolForCreateDto)
        {
            if (schoolForCreateDto.Logo != null &&
                !filesUploader.IsValidExtension(schoolForCreateDto.Logo.FileName, PhotoExtensions))
            {
                return(BadRequest("Logo posiada niepoprawny format"));
            }

            int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var currentUser   = await database.UserRepository.Get <User>(currentUserId);

            var school = mapper.Map <School>(schoolForCreateDto);

            if (currentUser.Rental != null)
            {
                school.SetRental(currentUser.Rental);
            }

            if (await schoolService.CreateSchool(school, currentUserId))
            {
                await SetLogoUrl(schoolForCreateDto.Logo, school);

                await notificationSystem.PushNotification(currentUserId, StaticExpressions.SchoolCreated(school.Name));

                await notificationSystem.PushNotificationToUsersByRoles(StaticExpressions.SchoolCreated(school.Owner.UserName, school.Name, school.Id),
                                                                        RolesPermitted, NotificationType.School);

                var schoolToReturn = mapper.Map <SchoolDetailsDto>(school);

                return(Ok(schoolToReturn));
            }

            return(BadRequest("Dodawanie szkółki nie powiodło się"));
        }
        public async Task <IActionResult> AddRentalOpinion(int rentalId, RentalOpinionForCreateDto rentalOpinionForCreateDto)
        {
            var rental = await database.RentalRepository.Get <Rental>(rentalId);

            var currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (rentalService.UserRentalOpinionExist(currentUserId, rental))
            {
                return(BadRequest("Nie można dodać kilku opinii"));
            }

            if (rental.OwnerId == currentUserId)
            {
                return(BadRequest("Nie można dodać opinii do swojej szkółki"));
            }

            var rentalOpinion = mapper.Map <RentalOpinion>(rentalOpinionForCreateDto);

            if (await rentalService.AddRentalOpinion(currentUserId, rental, rentalOpinion))
            {
                var rentalOpinionToReturn = mapper.Map <RentalOpinionDetailsDto>(rentalOpinion);

                await notificationSystem.PushNotification(rental.OwnerId, StaticExpressions.RentalOpinionCreatedForOwner(rentalOpinion.User.UserName));

                return(Ok(rentalOpinionToReturn));
            }

            return(BadRequest("Nie udało się dodać opinii dla wypożyczalni"));
        }
        public async Task <bool> CancelMeeting(int meetingId, int userId, int schoolId, int currentUserId, bool isRejected = false)
        {
            var meeting = await database.UserRepository.Get <Meeting>(meetingId);

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

            if (isRejected && currentUserId == meeting.ArrangedBy)
            {
                return(false);
            }

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

            if (!school.Meetings.Any(m => m.Id == meetingId && m.UserId == userId && m.SchoolId == schoolId))
            {
                return(false);
            }

            if (meeting.Accepted && isRejected)
            {
                return(false);
            }

            database.UserRepository.Remove(meeting);

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

            if (currentUserId == school.OwnerId)
            {
                if (!isRejected)
                {
                    await notificationSystem.PushNotification(userId, StaticExpressions.MeetingCancelledByOwner(school.Name));
                }
                else
                {
                    await notificationSystem.PushNotification(userId, StaticExpressions.MeetingRejectedByOwner(school.Name));
                }
            }
            else
            {
                if (!isRejected)
                {
                    await notificationSystem.PushNotification(school.OwnerId, StaticExpressions.MeetingCancelled);
                }
                else
                {
                    await notificationSystem.PushNotification(school.OwnerId, StaticExpressions.MeetingRejected);
                }
            }

            return(true);
        }
Exemple #13
0
        public void Expression_from_static_method_with_parameter()
        {
            using (var context = new SimpleModelContext())
            {
                var products = context.Products.Where(StaticExpressions.MethodWithParameter(1));

                products.ToList();
            }
        }
Exemple #14
0
        public void Expression_from_static_delegate()
        {
            using (var context = new SimpleModelContext())
            {
                var products = context.Products.Where(StaticExpressions.Delegate());

                products.ToList();
            }
        }
        public void Expression_from_static_delegate_with_parameter()
        {
            using (var context = new SimpleModelContext())
            {
                var products = context.Products.Where(StaticExpressions.DelegateWithParameter(1));

                Assert.DoesNotThrow(() => products.ToList());
            }
        }
        public void Expression_from_static_method_with_variable()
        {
            using (var context = new SimpleModelContext())
            {
                var products = context.Products.Where(StaticExpressions.MethodWithVariable());

                Assert.DoesNotThrow(() => products.ToList());
            }
        }
        public void Non_expression_embedded_static_method_throws()
        {
            using (var context = new SimpleModelContext())
            {
                var products = context.Products
                               .Where(p => p.Id == StaticExpressions.NonExpressionMethod());

                Assert.Throws <NotSupportedException>(() => products.ToList());
            }
        }
Exemple #18
0
        public void Expression_embedded_static_delegate_with_parameter()
        {
            using (var context = new SimpleModelContext())
            {
                var products = context.Products
                               .Where(p => context.Products.Where(StaticExpressions.DelegateWithParameter(1))
                                      .Contains(p));

                products.ToList();
            }
        }
        public void Expression_embedded_static_delegate()
        {
            using (var context = new SimpleModelContext())
            {
                var products = context.Products
                               .Where(p => context.Products.Where(StaticExpressions.Delegate())
                                      .Contains(p));

                Assert.DoesNotThrow(() => products.ToList());
            }
        }
        public void Expression_embedded_static_method_with_parameter()
        {
            using (var context = new SimpleModelContext())
            {
                var products = context.Products
                               .Where(p => context.Products.Where(StaticExpressions.MethodWithParameter(1))
                                      .Contains(p));

                Assert.DoesNotThrow(() => products.ToList());
            }
        }
        public void Non_expression_embedded_static_delegate_throws()
        {
            using (var context = new SimpleModelContext())
            {
                var testInstance = new InstanceExpressions();
                var products     = context.Products
                                   .Where(p => p.Id == StaticExpressions.NonExpressionDelegate());

                Assert.Throws <NotSupportedException>(() => products.ToList());
            }
        }
Exemple #22
0
        public void Expression_embedded_static_method_with_variable()
        {
            using (var context = new SimpleModelContext())
            {
                var products = context.Products
                               .Where(p => context.Products.Where(StaticExpressions.MethodWithVariable())
                                      .Contains(p));

                products.ToList();
            }
        }
        public async Task <bool> DenyRental(Rental rental)
        {
            if (rental == null)
            {
                return(false);
            }

            database.AdminRepository.Remove <Rental>(rental);

            await notificationSystem.PushNotification(rental.OwnerId, StaticExpressions.RentalDenied(rental.Name), NotificationType.Rental);

            return(await database.Complete());
        }
        public async Task <bool> DenySchool(School school)
        {
            if (school == null)
            {
                return(false);
            }

            database.AdminRepository.Remove(school);

            await notificationSystem.PushNotification(school.OwnerId, StaticExpressions.SchoolDenied(school.Name), NotificationType.School);

            return(true);
        }
        public async Task <bool> AcceptRental(Rental rental)
        {
            if (rental == null)
            {
                return(false);
            }

            rental.Accept();

            await rolesService.AdmitRole(rental.Owner, RoleName.RentalOwner);

            await notificationSystem.PushNotification(rental.OwnerId, StaticExpressions.RentalAccepted(rental.Name), NotificationType.Rental);

            return(true);
        }
        public async Task <bool> AcceptSchool(School school)
        {
            if (school == null)
            {
                return(false);
            }

            school.Accept();

            await rolesService.AdmitRole(school.Owner, RoleName.SchoolOwner);

            await notificationSystem.PushNotification(school.OwnerId, StaticExpressions.SchoolAccepted(school.Name), NotificationType.School);

            return(true);
        }
Exemple #27
0
        private async Task RemindAboutMeetings()
        {
            var meetingsToRemind = context.Meetings.Where(m => m.DateFrom <= DateTime.Now.AddDays(1) && m.Accepted && !m.Reminded);

            var userIds = meetingsToRemind.Select(m => new { m.UserId, m.School.OwnerId });

            userIds.ToList().ForEach(ui =>
            {
                var meeting = meetingsToRemind.FirstOrDefault(m => m.UserId == ui.UserId && m.School.OwnerId == ui.OwnerId);
                meeting.Remind();

                notificationSystem.PushNotification(ui.UserId, StaticExpressions.MeetingRemind(meeting.School.Name, meeting.DateFrom.ToShortDateString())).Wait();
                notificationSystem.PushNotification(ui.OwnerId, StaticExpressions.MeetingRemindForOwner(meeting.DateFrom.ToShortDateString())).Wait();
            });

            await context.SaveChangesAsync();
        }
Exemple #28
0
        public async Task <IActionResult> SendChangeEmail(int id, UserToChangeEmailDto userToChangeEmailDto)
        {
            var user = await accountManager.GetUser(id);

            if (await authService.GetUserByEmail(userToChangeEmailDto.Email) != null)
            {
                return(BadRequest("Użytkownik o takim adresie email już istnieje"));
            }

            var callbackUrl = await accountManager.GenerateEmailConfirmationCallbackUrl(user, userToChangeEmailDto.Email);

            if (await emailSender.SendEmail(StaticExpressions.ChangeEmailEmail(userToChangeEmailDto.Email, user.UserName, callbackUrl)))
            {
                return(Accepted());
            }

            throw new AccountException("Wystąpił błąd podczas zmiany adresu email");
        }
        public async Task <IActionResult> SendResetPassword([FromForm] string email)
        {
            var user = await authService.GetUserByEmail(email);

            if (user == null)
            {
                return(Accepted());
            }

            var callbackUrl = await authService.GenerateResetPasswordCallbackUrl(user);

            if (await emailSender.SendEmail(StaticExpressions.ResetPasswordEmail(user.Email, user.UserName, callbackUrl)))
            {
                return(Accepted());
            }

            throw new AuthorizationException("Wystąpił błąd podczas resetowania hasła");
        }
        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);
        }