예제 #1
0
        public void Handle(TakeParkingPlaceCommand command)
        {
            var parking = _dbContext.Set <Models.Parking>().FirstOrDefault(p => p.Name == command.ParkingName);

            if (parking == null)
            {
                throw new Exception($"Cannot find parking '{command.ParkingName}'.");
            }

            if (!parking.IsOpened)
            {
                throw new Exception($"The parking '{command.ParkingName}' is closed.");
            }

            var parkingPlace = _dbContext.Set <ParkingPlace>().FirstOrDefault(p => p.ParkingName == command.ParkingName && p.Number == command.PlaceNumber);

            if (parkingPlace == null)
            {
                throw new Exception($"Cannot find place #{command.PlaceNumber} in the parking '{command.ParkingName}'.");
            }

            if (!parkingPlace.IsFree)
            {
                throw new Exception($"Parking place #{command.PlaceNumber} is already taken.");
            }

            parkingPlace.IsFree = false;
            parkingPlace.UserId = _authenticationService.UserId;

            _dbContext.SaveChanges();
            _commandStoreService.Push(command);
        }
        public void Handle(CloseParkingCommand command)
        {
            var parking = _dbContext.Set <Models.Parking>()
                          .FirstOrDefault(p => p.Name == command.ParkingName);

            if (parking == null)
            {
                throw new Exception($"Cannot find parking '{command.ParkingName}'.");
            }
            if (!parking.IsOpened)
            {
                throw new Exception($"Parking '{command.ParkingName}' is already closed.");
            }

            parking.IsOpened = false;
            _dbContext.SaveChanges();

            _commandStoreService.Push(command);
        }
예제 #3
0
        public void Handle(AddPersonCommand command)
        {
            var person = new Person {
                FirstName  = command.FirstName,
                MiddleName = command.MiddleName,
                LastName   = command.LastName,
                NIN        = command.NIN,
                Dob        = command.Dob
            };

            _dbContext.Add(person);
            _dbContext.SaveChanges();

            _commandStoreService.Push(command);
        }
예제 #4
0
        public void Handle(CreateParkingCommand command)
        {
            var places = Enumerable.Range(1, command.Capacity)
                         .Select(number => new ParkingPlace
            {
                ParkingName = command.ParkingName,
                Number      = number,
                IsFree      = true
            }).ToList();

            var parking = new Models.Parking
            {
                Name     = command.ParkingName,
                IsOpened = true,
                Places   = places
            };

            _dbContext.Add(parking);
            _dbContext.SaveChanges();

            // Taking command handling all the way will remove the dependency to the command store service
            _commandStoreService.Push(command);
        }