Exemplo n.º 1
0
        public async Task <VoteDTO> Vote(VoteCreateDTO newVote, int tripId)
        {
            using (_unitOfWork)
            {
                Vote    vote    = _mapper.Map <VoteCreateDTO, Vote>(newVote);
                Votable votable = await _unitOfWork.VotableRepository.GetVotableWithVotes(newVote.VotableId);

                if (votable.Votes.Where(x => x.UserId == newVote.UserId).FirstOrDefault() != null)
                {
                    return(null);
                }

                votable.Votes.Add(vote);
                if (newVote.Positive)
                {
                    votable.PositiveVotes++;
                }
                else
                {
                    votable.NegativeVotes++;
                }
                vote.Votable = votable;

                _unitOfWork.VotableRepository.Update(votable);
                await _unitOfWork.VoteRepository.Create(vote);

                await _unitOfWork.Save();

                await _messageControllerService.NotifyOnTripChanges(tripId, "ChangeVotable", _mapper.Map <Votable, VotableDTO>(votable));

                return(_mapper.Map <Vote, VoteDTO>(vote));
            }
        }
Exemplo n.º 2
0
        public async Task <ItemDTO> CreateItem(ItemCreateDTO newItem)
        {
            using (_unitOfWork)
            {
                Item item = _mapper.Map <ItemCreateDTO, Item>(newItem);
                User user = await _unitOfWork.UserRepository.GetUserWithItems(newItem.UserId);

                Trip trip = await _unitOfWork.TripRepository.GetTripWithItemsAndMembers(newItem.TripId);

                if (!trip.Travelers.Contains(user))
                {
                    return(null);
                }

                item.User = user;
                item.Trip = trip;

                if (user.MyItems == null)
                {
                    user.MyItems = new List <Item>();
                }
                user.MyItems.Add(item);

                if (trip.ItemList == null)
                {
                    trip.ItemList = new List <Item>();
                }
                trip.ItemList.Add(item);

                await _unitOfWork.ItemRepository.Create(item);

                _unitOfWork.UserRepository.Update(user);
                _unitOfWork.TripRepository.Update(trip);

                Notification notification = new Notification();
                notification.Seen = false;
                notification.RelatedObjectName = item.Name;
                notification.Type   = NotificationType.AddedItem;
                notification.User   = user;
                notification.UserId = user.UserId;
                await _unitOfWork.NotificationRepository.Create(notification);

                await _unitOfWork.Save();

                ItemDTO retItem = _mapper.Map <Item, ItemDTO>(item);
                await _messageControllerService.NotifyOnTripChanges(newItem.TripId, "AddItem", retItem);

                NotificationItemDTO notificationItem = new NotificationItemDTO()
                {
                    Notification = _mapper.Map <Notification, NotificationDTO>(notification),
                    Item         = retItem
                };
                await _messageControllerService.SendNotification(newItem.UserId, "AddItemNotification", notificationItem);

                return(retItem);
            }
        }
Exemplo n.º 3
0
        public async Task <AddOnDTO> EditAddOn(AddOnEditDTO addOnInfo, int tripId)
        {
            using (_unitOfWork)
            {
                AddOn addOn = await _unitOfWork.AddOnRepository.GetAddOnWithVotable(addOnInfo.AddOnId);

                addOn.Price       = addOnInfo.Price;
                addOn.Description = addOnInfo.Description;
                _unitOfWork.AddOnRepository.Update(addOn);
                await _unitOfWork.Save();

                AddOnDTO retValue = _mapper.Map <AddOn, AddOnDTO>(addOn);
                await _messageControllerService.NotifyOnTripChanges(tripId, "EditAddOn", retValue);

                return(retValue);
            }
        }
        public async Task <AccommodationDTO> CreateAccommodation(AccommodationCreateDTO newAccommodation)
        {
            using (_unitOfWork)
            {
                if (!DateManagerService.checkFromToDates(newAccommodation.From, newAccommodation.To))
                {
                    return(null);
                }

                Accommodation accommodation = _mapper.Map <AccommodationCreateDTO, Accommodation>(newAccommodation);
                Location      location      = await _unitOfWork.LocationRepository.FindByID(newAccommodation.LocationId);

                if (!DateManagerService.checkParentChildDates(location.From, location.To, accommodation.From, accommodation.To))
                {
                    return(null);
                }

                accommodation.Location = location;

                if (location.Accommodations == null)
                {
                    location.Accommodations = new List <Accommodation>();
                }
                location.Accommodations.Add(accommodation);

                Votable votable = new Votable();
                accommodation.Votable = votable;

                await _unitOfWork.VotableRepository.Create(votable);

                await _unitOfWork.AccommodationRepository.Create(accommodation);

                await _unitOfWork.Save();

                AccommodationDTO retValue = _mapper.Map <Accommodation, AccommodationDTO>(accommodation);
                await _messageControllerService.NotifyOnTripChanges(location.TripId, "AddAccommodation", retValue);

                return(retValue);
            }
        }
        public async Task <LocationDTO> CreateLocation(LocationCreateDTO newLocation)
        {
            using (_unitOfWork)
            {
                if (!DateManagerService.checkFromToDates(newLocation.From, newLocation.To))
                {
                    return(null);
                }

                Location location = _mapper.Map <LocationCreateDTO, Location>(newLocation);
                Trip     trip     = await _unitOfWork.TripRepository.FindByID(location.TripId);

                if (!DateManagerService.checkParentChildDates(trip.From, trip.To, location.From, location.To))
                {
                    return(null);
                }

                location.Trip = trip;

                if (trip.Locations == null)
                {
                    trip.Locations = new List <Location>();
                }
                trip.Locations.Add(location);

                Votable votable = new Votable();
                location.Votable = votable;

                await _unitOfWork.VotableRepository.Create(votable);

                await _unitOfWork.LocationRepository.Create(location);

                await _unitOfWork.Save();

                LocationDTO retValue = _mapper.Map <Location, LocationDTO>(location);
                await _messageControllerService.NotifyOnTripChanges(location.TripId, "AddLocation", retValue);

                return(retValue);
            }
        }
Exemplo n.º 6
0
        public async Task <TripDTO> EditTripInfo(TripEditDTO tripInfo)
        {
            using (_unitOfWork)
            {
                if (!DateManagerService.checkFromToDates(tripInfo.From, tripInfo.To))
                {
                    return(null);
                }

                Trip trip = await _unitOfWork.TripRepository.GetTripWithILocations(tripInfo.TripId);

                if (trip.Locations != null)
                {
                    foreach (Location location in trip.Locations)
                    {
                        if (!DateManagerService.checkParentChildDates(tripInfo.From, tripInfo.To, location.From, location.To))
                        {
                            return(null);
                        }
                    }
                }

                trip.Name        = tripInfo.Name;
                trip.Description = tripInfo.Description;
                trip.From        = tripInfo.From;
                trip.To          = tripInfo.To;
                _unitOfWork.TripRepository.Update(trip);
                await _unitOfWork.Save();

                TripDTO      returnTrip       = _mapper.Map <Trip, TripDTO>(trip);
                TripBasicDTO notificationTrip = _mapper.Map <Trip, TripBasicDTO>(trip);
                await _messageControllerService.NotifyOnTripChanges(trip.TripId, "EditTripInfo", notificationTrip);

                return(returnTrip);
            }
        }