protected override void Handle(AssignWorkerServicesCommand request)
        {
            var worker = _dbContext.Workers.Include(w => w.Services).FirstOrDefault(w => w.Id.ToString() == request.WorkerId);

            if (worker == null)
            {
                throw new ApplicationException("Cannot assign services because worker with id=" + request.WorkerId + "do not exists.");
            }

            worker.ClearAssignedServices();

            var servicesToAssign = _dbContext.Services.Where(ser => ser.SalonId == worker.SalonId && request.Services.Select(s => s.Id).Contains(ser.Id.ToString()));

            if (servicesToAssign.Any())
            {
                foreach (var service in servicesToAssign)
                {
                    worker.AssignService(service);
                }
            }

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not save assigned services to worker into database.");
            }
        }
Exemplo n.º 2
0
        protected override void Handle(DeleteOpinionCommand request)
        {
            Guid opinionId = new Guid(request.Id);

            var opinion = _dbContext.Opinions.FirstOrDefault(s => s.Id == opinionId);

            if (opinion == null)
            {
                throw new ApplicationException("Could not find opinion with id=" + opinionId);
            }
            var worker = _dbContext.Workers
                         .FirstOrDefault(w => w.Id == opinion.WorkerId);
            var workerSalon = _dbContext.Salons.Include(s => s.Workers).FirstOrDefault(s => s.Id == worker.SalonId);

            worker.Salon = workerSalon;
            if (worker == null)
            {
                throw new ApplicationException("Could not find worker with id=" + opinion.WorkerId);
            }
            worker.RevertRating(opinion.Rate);
            var image = _dbContext.Images.FirstOrDefault(i => i.Id == opinion.Id);

            if (image != null)
            {
                _dbContext.Images.Remove(image);
            }

            _dbContext.Opinions.Remove(opinion);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new ApplicationException("Could not delete opinion from database.");
            }
        }
Exemplo n.º 3
0
        public SalonDto CreateSalon(SalonCreationDto salonCreation, IdentityUser admin)
        {
            var existingSalon = _dbContext.Salons.FirstOrDefault(s => s.Id.ToString() == admin.Id);

            if (existingSalon != null)
            {
                throw new ApplicationException("Salon for user [id=" + admin.Id + "] already exists");
            }

            var schedule = _mapper.Map <Schedule>(salonCreation.Schedule);

            var resolvedImage = ImageService.ResolveToImage(salonCreation.ImageData);

            var salon = new Salon(admin,
                                  salonCreation.Name,
                                  _mapper.Map <Address>(salonCreation.Address),
                                  _mapper.Map <Location>(salonCreation.Location),
                                  salonCreation.AdditionalInfo,
                                  salonCreation.Type,
                                  schedule,
                                  resolvedImage.Source,
                                  resolvedImage.Header);

            _dbContext.Salons.Add(salon);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new DomainException("Could not save salon into database.");
            }

            return(_mapper.Map <SalonDto>(salon));
        }
Exemplo n.º 4
0
        protected override OpinionDto Handle(CreateOpinionCommand request)
        {
            var client = _dbContext.Clients.FirstOrDefault(c => c.Id.ToString() == request.ClientId);

            if (client == null)
            {
                throw new ApplicationException("Could not add opinion, client with id=" + request.ClientId + " not found.");
            }
            var worker = _dbContext.Workers
                         .FirstOrDefault(w => w.Id.ToString() == request.WorkerId);
            var workerSalon = _dbContext.Salons.Include(s => s.Workers).FirstOrDefault(s => s.Id == worker.SalonId);

            worker.Salon = workerSalon;
            if (worker == null)
            {
                throw new ApplicationException("Could not add opinion, worker with id=" + request.WorkerId + " not found.");
            }
            var visit = _dbContext.Visits.FirstOrDefault(v => v.Id.ToString() == request.VisitId);

            if (visit == null)
            {
                throw new ApplicationException("Could not add opinion, visit with id=" + request.VisitId + " not found.");
            }
            visit.SetOpinionSent(true);
            visit.UpdateInfo("Opinion fo this visit has been sent.");

            var resolvedImage = ImageService.ResolveToImage(request.ImageSource);

            var opinion = new Domain.Entities.Opinion(
                new Guid(request.ClientId),
                new Guid(request.WorkerId),
                request.Description,
                request.Rate,
                resolvedImage.Source,
                resolvedImage.Header);

            worker.UpdateRating(request.Rate);
            _dbContext.Opinions.Add(opinion);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not save created opinion into database.");
            }

            var result = _mapper.Map <OpinionDto>(opinion);

            result.ClientId        = client.Id.ToString();
            result.ClientFirstName = client.FirstName;
            result.ClientLastName  = client.LastName;
            result.WorkerId        = worker.Id.ToString();
            result.WorkerFirstName = worker.FirstName;
            result.WorkerLastName  = worker.LastName;

            return(result);
        }
Exemplo n.º 5
0
        protected override void Handle(UploadImageCommand request)
        {
            var resolvedImage = ImageService.ResolveToImage(request.ImageSource);
            var image         = _dbContext.Images.FirstOrDefault(i => i.Id == new Guid(request.Id));

            if (image == null)
            {
                throw new ApplicationException("Could not upload image. Not found refernce for this entity in Images table.");
            }
            if (image.Update(resolvedImage.Source, resolvedImage.Header))
            {
                if (_dbContext.SaveChanges() == 0)
                {
                    throw new Exception("Could not save image into database.");
                }
            }
        }
Exemplo n.º 6
0
        protected override void Handle(DeleteServiceCommand request)
        {
            Guid serviceId = new Guid(request.Id);

            var service = _dbContext.Services
                          .Include(s => s.VisitsHistory).ThenInclude(vs => vs.Visit).ThenInclude(v => v.Services)
                          .FirstOrDefault(s => s.Id == serviceId);

            if (service == null)
            {
                throw new ApplicationException("Could not find service with id=" + serviceId);
            }

            var activeVisits = service.VisitsHistory.Select(vs => vs.Visit).Where(v => (v.Status != Domain.Enums.VisitStatus.Rejected) && v.Term > DateTime.Now);

            if (activeVisits.Any())
            {
                throw new ApplicationException("Before delete service, workers have to reject all pending/accpeted/change-requested upcoming visits which provides this service.");
            }

            if (service.VisitsHistory.Any())
            {
                foreach (var visit in service.VisitsHistory.Select(vs => vs.Visit))
                {
                    _dbContext.VisitServices.RemoveRange(visit.Services);
                    _dbContext.Visits.Remove(visit);
                }
            }

            var salonWorkers = _dbContext.Workers
                               .Include(w => w.Services)
                               .Where(w => w.SalonId == service.SalonId);

            foreach (var worker in salonWorkers)
            {
                worker.RemoveAssignedService(service);
            }

            _dbContext.Services.Remove(service);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new ApplicationException("Could not delete service from database.");
            }
        }
Exemplo n.º 7
0
        protected override ServiceDto Handle(CreateServiceCommand request)
        {
            var service = new Service(
                request.Name,
                request.Price,
                request.Time,
                new Guid(request.SalonId)
                );

            _dbContext.Services.Add(service);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not save created service into database.");
            }

            return(_mapper.Map <ServiceDto>(service));
        }
        protected override void Handle(UpdateOpinionAnswerCommand request)
        {
            Guid opinionId = new Guid(request.Id);

            var opinion = _dbContext.Opinions.FirstOrDefault(s => s.Id == opinionId);

            if (opinion == null)
            {
                throw new ApplicationException("Could not find opinion with id=" + opinionId);
            }

            opinion.SetAnswer(request.Answer);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new ApplicationException("Could not update opinion in database.");
            }
        }
        protected override void Handle(UpdateWorkerDataCommand request)
        {
            Guid workerId = new Guid(request.Id.ToString());

            var worker = _dbContext.Workers.FirstOrDefault(s => s.Id == workerId);

            if (worker == null)
            {
                throw new ApplicationException("Could not find worker with id=" + workerId);
            }

            if (worker.UpdateData(request.FirstName, request.LastName))
            {
                if (_dbContext.SaveChanges() == 0)
                {
                    throw new Exception("Could not save worker data changes into database.");
                }
            }
        }
Exemplo n.º 10
0
        protected override VisitDto Handle(ConfirmVisitCommand request)
        {
            var visit = _dbContext.Visits.Include(v => v.Worker).FirstOrDefault(v => v.Id == new Guid(request.Id));

            if (visit == null)
            {
                throw new ApplicationException("Could not find visit with id=" + request.Id);
            }

            if (visit.Term < DateTime.Now)
            {
                throw new ApplicationException("Could not change old visits");
            }

            var thisDayOtherWorkerVisits = _dbContext.Visits.
                                           Where(v => v.Worker.Id == visit.Worker.Id &&
                                                 v.Status != VisitStatus.Rejected &&
                                                 v.Term.Date == visit.Term.Date &&
                                                 v.Id != visit.Id);

            if (thisDayOtherWorkerVisits.Any())
            {
                var collidingVisits = thisDayOtherWorkerVisits.
                                      Where(v => (v.Term <= visit.Term && v.Term.AddMinutes(v.TotalTime) > visit.Term) ||
                                            (v.Term > visit.Term && v.Term < visit.Term.AddMinutes(visit.TotalTime)));

                if (collidingVisits.Any())
                {
                    throw new ApplicationException("Could not confirm visit. Worker has other not rejected visits at this time.");
                }
            }

            visit.SetStatus(VisitStatus.Accepted);
            visit.SetInfo("Visit confirmed.");


            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not save visit changes into database.");
            }

            return(_mapper.Map <VisitDto>(visit));
        }
Exemplo n.º 11
0
        protected override void Handle(UpdateScheduleCommand request)
        {
            Guid scheduleId = new Guid(request.Id.ToString());
            var  schedule   = _dbContext.Schedules.FirstOrDefault(i => i.Id == scheduleId);

            if (schedule == null)
            {
                throw new ApplicationException("Could not update schedule. Not found refernce for this entity in Schedules table.");
            }
            var newSchedule = _mapper.Map <Schedule>(request.Schedule);

            if (schedule.Update(newSchedule))
            {
                if (_dbContext.SaveChanges() == 0)
                {
                    throw new Exception("Could not save schedule changes into database.");
                }
            }
        }
Exemplo n.º 12
0
        protected override VisitDto Handle(CreateVisitCommand request)
        {
            var services = _dbContext.Services.Where(s => request.ServiceIds.Contains(s.Id.ToString())).ToArray();

            if (services == null)
            {
                throw new ApplicationException("Could not add visit with no services defined.");
            }

            var visit = new Domain.Entities.Visit(
                new Guid(request.ClientId),
                new Guid(request.WorkerId),
                services,
                new DateTimeOffset(request.Term).LocalDateTime,
                request.TotalTime,
                request.TotalPrice);

            _dbContext.Visits.Add(visit);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not save created visit into database.");
            }

            var worker = _dbContext.Workers
                         .Include(w => w.User)
                         .FirstOrDefault(w => w.Id.ToString() == request.WorkerId);

            var result = _mapper.Map <VisitDto>(visit);

            result.WorkerEmail       = worker.User.Email;
            result.WorkerFirstName   = worker.FirstName;
            result.WorkerId          = worker.Id.ToString();
            result.WorkerLastName    = worker.LastName;
            result.WorkerPhoneNumber = worker.User.PhoneNumber;
            result.WorkerRating      = worker.Rating;
            result.WorkerUserName    = worker.User.UserName;

            return(result);
        }
Exemplo n.º 13
0
        protected override void Handle(AddFavouriteSalonCommand request)
        {
            var client = _dbContext.Clients.Include(c => c.FavoriteSalons).FirstOrDefault(c => c.Id.ToString() == request.clientId);

            if (client == null)
            {
                throw new ApplicationException("Could not add favourite salon. Not found refernce for client with id=" + request.clientId);
            }
            var salon = _dbContext.Salons.FirstOrDefault(s => s.Id.ToString() == request.salonId);

            if (salon == null)
            {
                throw new ApplicationException("Could not add favourite salon. Not found refernce for salon with id=" + request.salonId);
            }

            if (client.AddFavouriteSalon(salon))
            {
                if (_dbContext.SaveChanges() == 0)
                {
                    throw new Exception("Could not add favourite salon to client.");
                }
            }
        }
Exemplo n.º 14
0
        protected override VisitDto Handle(RejectVisitCommand request)
        {
            var visit = _dbContext.Visits.FirstOrDefault(v => v.Id == new Guid(request.Id));

            if (visit == null)
            {
                throw new ApplicationException("Could not find visit with id=" + request.Id);
            }

            if (visit.Term < DateTime.Now)
            {
                throw new ApplicationException("Could not change old visits");
            }

            visit.SetStatus(VisitStatus.Rejected);

            var info = "Visit rejected by ";

            if (request.IsWorkerRejecting)
            {
                info += "hairdresser.";
            }
            else
            {
                info += "client.";
            }

            visit.SetInfo(info);


            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not save visit changes into database.");
            }

            return(_mapper.Map <VisitDto>(visit));
        }
Exemplo n.º 15
0
        protected override async Task Handle(CreateWorkerCommand request, CancellationToken cancellationToken)
        {
            var user = await _userService.CreateUserAsync(request.UserData);

            var identityUser = await _userManager.FindByIdAsync(user.Id);

            var resolvedImage = ImageService.ResolveToImage(request.ImageData);

            var worker = new Worker(
                identityUser,
                request.FirstName,
                request.LastName,
                new Guid(request.SalonId),
                _mapper.Map <Schedule>(request.Schedule),
                resolvedImage.Source,
                resolvedImage.Header);

            _dbContext.Workers.Add(worker);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not save created worker into database.");
            }
        }
Exemplo n.º 16
0
        protected override void Handle(UpdateSalonDataCommand request)
        {
            Guid salonId = new Guid(request.Id.ToString());

            var salon = _dbContext.Salons.FirstOrDefault(s => s.Id == salonId);

            if (salon == null)
            {
                throw new ApplicationException("Could not find salon with id=" + salonId);
            }

            if (salon.UpdateData(
                    request.Name,
                    request.AdditionalInfo,
                    _mapper.Map <Address>(request.Address),
                    _mapper.Map <Location>(request.Location),
                    request.Type))
            {
                if (_dbContext.SaveChanges() == 0)
                {
                    throw new Exception("Could not save salon data changes into database.");
                }
            }
        }
Exemplo n.º 17
0
        protected override void Handle(DeleteWorkerCommand request)
        {
            Guid workerId = new Guid(request.Id);

            var worker = _dbContext.Workers
                         .Include(w => w.User)
                         /*.Include(w => w.Salon).ThenInclude(s => s.Workers)*/
                         .Include(w => w.Services)
                         .Include(w => w.Visits).ThenInclude(v => v.Services)
                         .Include(w => w.Opinions)
                         .FirstOrDefault(s => s.Id == workerId);

            if (worker == null)
            {
                throw new ApplicationException("Could not find worker with id=" + workerId);
            }

            var activeVisits = worker.Visits.Where(v => (v.Status != Domain.Enums.VisitStatus.Rejected) && v.Term > DateTime.Now);

            if (activeVisits.Any())
            {
                throw new ApplicationException("Before delete worker, he has to reject all his pending/accpeted/change-requested upcoming visits");
            }

            //var workerSalon = _dbContext.Salons.FirstOrDefault(s => s.Id == worker.SalonId);

            if (worker.Opinions.Any())
            {
                var workerSalon = _dbContext.Salons.Include(s => s.Workers).FirstOrDefault(s => s.Id == worker.SalonId);
                worker.Salon = workerSalon;
                foreach (var opinion in worker.Opinions)
                {
                    worker.RevertRating(opinion.Rate);
                    var opinionImage = _dbContext.Images.FirstOrDefault(i => i.Id == opinion.Id);
                    if (opinionImage != null)
                    {
                        _dbContext.Images.Remove(opinionImage);
                    }
                    _dbContext.Opinions.Remove(opinion);
                }
            }

            if (worker.Visits.Any())
            {
                foreach (var visit in worker.Visits)
                {
                    _dbContext.VisitServices.RemoveRange(visit.Services);
                    _dbContext.Visits.Remove(visit);
                }
            }

            var image    = _dbContext.Images.FirstOrDefault(i => i.Id == workerId);
            var schedule = _dbContext.Schedules.FirstOrDefault(s => s.Id == workerId);
            var user     = _dbContext.Users.FirstOrDefault(u => u.Id == workerId.ToString());

            if (image != null)
            {
                _dbContext.Images.Remove(image);
            }
            if (schedule != null)
            {
                _dbContext.Schedules.Remove(schedule);
            }
            if (user != null)
            {
                _dbContext.Users.Remove(user);
            }

            worker.ClearAssignedServices();

            _dbContext.Workers.Remove(worker);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new ApplicationException("Could not delete worker from database.");
            }
        }
Exemplo n.º 18
0
        protected override VisitDto Handle(TermChangeRequestCommand request)
        {
            var visit = _dbContext.Visits.Include(v => v.Worker).FirstOrDefault(v => v.Id == new Guid(request.VisitId));

            if (visit == null)
            {
                throw new ApplicationException("Could not find visit with id=" + request.VisitId);
            }

            if (visit.Term < DateTime.Now)
            {
                throw new ApplicationException("Could not change old visits");
            }

            request.Term = new DateTimeOffset(request.Term).LocalDateTime;

            if (request.Term < DateTime.Now)
            {
                throw new ApplicationException("Could not change visit date to past date");
            }

            var thisOtherDayWorkerVisits = _dbContext.Visits.
                                           Where(v => v.Worker.Id == visit.Worker.Id &&
                                                 v.Status != VisitStatus.Rejected &&
                                                 v.Term.Date == request.Term.Date &&
                                                 v.Id != visit.Id);

            if (thisOtherDayWorkerVisits.Any())
            {
                var collidingVisits = thisOtherDayWorkerVisits.
                                      Where(v => (v.Term <= request.Term && v.Term.AddMinutes(v.TotalTime) > request.Term) ||
                                            (v.Term > request.Term && v.Term < request.Term.AddMinutes(visit.TotalTime)));

                if (collidingVisits.Any())
                {
                    throw new ApplicationException("Could not change visit date to already occupied date");
                }
            }

            var visitTermString = visit.Term.ToString(new CultureInfo("pl-PL"));

            visitTermString = visitTermString.Substring(0, visitTermString.Length - 3);
            var requestTermString = request.Term.ToString(new CultureInfo("pl-PL"));

            requestTermString = requestTermString.Substring(0, requestTermString.Length - 3);
            var info = "requests to change date of the visit"
                       + " from: " + visitTermString
                       + " to: " + requestTermString + ".";

            visit.SetTerm(request.Term);
            if (request.IsWorkerRequesting)
            {
                visit.SetStatus(VisitStatus.WorkerChangeRequested);
                visit.SetInfo("Hairdresser " + info);
            }
            else
            {
                if (visit.Status != VisitStatus.Pending)
                {
                    visit.SetStatus(VisitStatus.ClientChangeRequested);
                    visit.SetInfo("Client " + info);
                }
            }

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not save visit changes into database.");
            }

            return(_mapper.Map <VisitDto>(visit));
        }