コード例 #1
0
        public Rent AddRent(RentDto rent)
        {
            var rentToSave  = Mapper.Map <Rent>(rent);
            var offer       = _dbContext.Offers.FirstOrDefault(o => o.Id == rent.OfferId);
            var borrower    = _dbContext.Users.FirstOrDefault(u => u.Id == rent.BorrowerId);
            var rentingUser = _dbContext.Users.FirstOrDefault(r => r.Id == offer.OwnerId);

            borrower.Rents.Add(rentToSave);
            offer.Status = OfferStatus.Rented;

            // delete all remaining rentRequests of this offer
            IList <Notification> rentRequestsToRemove = _dbContext.Notifications
                                                        .Where(n => n.OfferId == rent.OfferId &&
                                                               n.Type == NotificationType.RentRequest &&
                                                               n.TargetUserId != borrower.Id).ToList <Notification>();

            _pointsService.ModifyPoints(rentingUser, new PointsModificationToolRentingStrategy());
            _pointsService.ModifyPoints(borrower, new PointsModificationToolBorrowingStrategy());


            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not add rent");
            }

            foreach (var rentRequest in rentRequestsToRemove)
            {
                // need notificationService to return deposit
                _notificationService.DeleteNotification(rentRequest.Id);
            }

            return(rentToSave);
        }
コード例 #2
0
        public NotificationDto AddNotification(NotificationForCreationDto notification)
        {
            var newNotification = Mapper.Map <Notification>(notification);

            _dbContext.Notifications.Add(newNotification);

            if (newNotification.Type == NotificationType.RentRequest)
            {
                var borrower = _dbContext.Users.FirstOrDefault(u => u.Id == notification.TargetUserId);
                _pointsService.ModifyPoints(borrower, new PointsModificationGetDepositStrategy());
            }

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not add notification");
            }

            return(Mapper.Map <NotificationDto>(newNotification));
        }
コード例 #3
0
        public UserDto Register(RegisterCredentialsDto registerCredentials)
        {
            var userToSave = Mapper.Map <User>(registerCredentials);

            _pointsService.ModifyPoints(userToSave, new PointsModificationRegistrationStrategy());

            _dbContext.Add(userToSave);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not register user");
            }

            return(Mapper.Map <UserDto>(userToSave));
        }
コード例 #4
0
        public OfferDto AddOffer(OfferForCreationDto offer, Guid userId)
        {
            var offerToSave = Mapper.Map <Offer>(offer);

            var user = _dbContext.Users.FirstOrDefault(u => u.Id == userId);

            user.Offers.Add(offerToSave);

            _pointsService.ModifyPoints(user, new PointsModificationOfferCreationStrategy());

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not add offer");
            }

            return(Mapper.Map <OfferDto>(offerToSave));
        }
コード例 #5
0
        public OpinionDto AddOpinion(OpinionForCreationDto opinion, Guid ratedUserId, Guid ratingUserId)
        {
            var opinionToSave = Mapper.Map <Opinion>(opinion);

            var ratingUser = _dbContext.Users.FirstOrDefault(u => u.Id == ratingUserId);

            ratingUser.GivenOpinions.Add(opinionToSave);

            _dbContext.Users.FirstOrDefault(u => u.Id == ratedUserId).ReceivedOpinions.Add(opinionToSave);
            _dbContext.Opinions.Add(opinionToSave);

            _pointsService.ModifyPoints(ratingUser, new PointsModificationOpinionAdditionStrategy());

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not add opinion");
            }

            return(Mapper.Map <OpinionDto>(opinionToSave));
        }