Beispiel #1
0
        public void Cancel()
        {
            if (!Equals(_status, TripStatus.Created) || !Equals(_status, TripStatus.Accepted))
            {
                throw new TripDomainInvalidOperationException($"Invalid trip status to cancel the trip. Current status: {_status.Name}");
            }

            _end    = DateTime.UtcNow;
            _status = TripStatus.Cancelled;

            //let's say there is a business rule that says when cancelling the rating is 2 for both user an driver.
            _rating = new Rating(2, 2);

            AddEvent(new TripUpdatedDomainEvent
            {
                AggregateRootId = Id,
                Action          = Action.Cancelled,
                Status          = _status,
                Started         = _start,
                Ended           = _end,
                PaymentMethod   = _paymentMethod,
                Duration        = GetDuration(),
                UserTripId      = _userId
            });
        }
Beispiel #2
0
 private void Apply(TripUpdatedDomainEvent @event)
 {
     _start           = @event.Started;
     _end             = @event.Ended;
     _status          = @event.Status;
     _currentLocation = @event.CurrentLocation;
 }
Beispiel #3
0
        public void SetCurrentLocation(Location currentLocation)
        {
            if (!Equals(_status, TripStatus.InCourse))
            {
                throw new TripDomainInvalidOperationException($"Invalid trip status to set the current location. Current status: {_status.Name}");
            }

            _currentLocation = currentLocation ?? throw new TripDomainArgumentNullException(nameof(currentLocation));

            // TODO: handle a tolerance range to determine if current location is the destination
            if (Equals(currentLocation, _to))
            {
                _end    = DateTime.UtcNow;
                _status = TripStatus.Finished;
            }

            AddEvent(new TripUpdatedDomainEvent
            {
                AggregateRootId = Id,
                Action          = Action.UpdatedCurrentLocation,
                Status          = _status,
                Started         = _start,
                Ended           = _end,
                CurrentLocation = currentLocation,
                Duration        = GetDuration(),
                Distance        = GetDistance(),
                PaymentMethod   = _paymentMethod,
                UserTripId      = _userId
            });
        }
        public void FinishEarlier()
        {
            if (!Equals(_status, TripStatus.InCourse))
            {
                throw new TripDomainInvalidOperationException($"Invalid trip status to finish the trip. Current status: {_status.Name}");
            }

            _end    = DateTime.UtcNow;
            _status = TripStatus.Finished;
            _to     = _currentLocation;

            AddEvent(new TripUpdatedDomainEvent
            {
                AggregateRootId = Id,
                Action          = Action.FinishedEarlier,
                Status          = _status,
                Started         = _start,
                Ended           = _end,
                Duration        = GetDuration(),
                Distance        = GetDistance(),
                PaymentMethod   = _paymentMethod,
                UserTripId      = _userId,
                ConnectionId    = _connectionId
            });
        }
Beispiel #5
0
 private void Apply(TripUpdated @event)
 {
     Id               = @event.AggregateRootId;
     _end             = @event.Ended;
     _start           = @event.Started;
     _status          = @event.Status;
     _currentLocation = @event.CurrentLocation;
 }
Beispiel #6
0
        public Trip(Guid id, int userId, int driverId, Location from, Location to, PaymentMethod paymentMethod, string plate, string brand, string model) : base(id)
        {
            if (userId <= 0)
            {
                throw new TripDomainArgumentNullException(nameof(userId));
            }
            if (driverId <= 0)
            {
                throw new TripDomainArgumentNullException(nameof(driverId));
            }
            if (string.IsNullOrWhiteSpace(plate))
            {
                throw new TripDomainArgumentNullException(nameof(plate));
            }
            if (string.IsNullOrWhiteSpace(brand))
            {
                throw new TripDomainArgumentNullException(nameof(brand));
            }
            if (string.IsNullOrWhiteSpace(model))
            {
                throw new TripDomainArgumentNullException(nameof(model));
            }
            if (from == null)
            {
                throw new TripDomainArgumentNullException(nameof(from));
            }
            if (to == null)
            {
                throw new TripDomainArgumentNullException(nameof(to));
            }

            if (Equals(from, to))
            {
                throw new TripDomainInvalidOperationException("Destination and origin can't be the same.");
            }

            _paymentMethod      = paymentMethod ?? throw new TripDomainArgumentNullException(nameof(paymentMethod));
            _create             = DateTime.UtcNow;
            _status             = TripStatus.Created;
            _userId             = userId;
            _driverId           = driverId;
            _from               = from;
            _to                 = to;
            _vehicleInformation = new VehicleInformation(plate, brand, model);

            AddEvent(new TripCreatedDomainEvent
            {
                AggregateRootId    = Id,
                VehicleInformation = _vehicleInformation,
                UserTripId         = _userId,
                DriverId           = _driverId,
                From          = _from,
                To            = _to,
                PaymentMethod = _paymentMethod,
                TimeStamp     = _create,
                Status        = _status
            });
        }
Beispiel #7
0
 public void Accept()
 {
     _status = TripStatus.Accepted;
     AddEvent(new TripUpdated
     {
         AggregateRootId = Id,
         Action          = Action.Accepted,
         Status          = _status
     });
 }
Beispiel #8
0
 // Applies events after load an object from event store. (kinda memento pattern)
 private void Apply(TripCreatedDomainEvent @event)
 {
     Id                  = @event.AggregateRootId;
     _status             = @event.Status;
     _create             = @event.TimeStamp;
     _driverId           = @event.DriverId;
     _from               = @event.From;
     _to                 = @event.To;
     _userId             = @event.UserTripId;
     _vehicleInformation = @event.VehicleInformation;
     _paymentMethod      = @event.PaymentMethod;
 }
Beispiel #9
0
        public void Accept()
        {
            if (!Equals(_status, TripStatus.Created))
            {
                throw new TripDomainInvalidOperationException($"Invalid trip status to accept the trip. Current status: {_status.Name}");
            }

            _status = TripStatus.Accepted;
            AddEvent(new TripUpdatedDomainEvent
            {
                AggregateRootId = Id,
                Action          = Action.Accepted,
                Status          = _status
            });
        }
Beispiel #10
0
        public void FinishEarlier()
        {
            _end    = DateTime.UtcNow;
            _status = TripStatus.Finished;
            _to     = _currentLocation;

            AddEvent(new TripUpdated
            {
                AggregateRootId = Id,
                Action          = Action.FinishedEarlier,
                Status          = _status,
                Started         = _start,
                Ended           = _end
            });
        }
Beispiel #11
0
        public void Start()
        {
            if (!Equals(_status, TripStatus.Accepted))
            {
                throw new TripDomainInvalidOperationException($"Before to start the trip, it should be accepted. Current status: {_status.Name}");
            }

            _start = DateTime.UtcNow;

            // we're assuming that the driver already picked up the user.
            _status = TripStatus.InCourse;

            AddEvent(new TripUpdatedDomainEvent
            {
                AggregateRootId = Id,
                Action          = Action.Started,
                Status          = _status,
                Started         = _start
            });
        }