Esempio n. 1
0
        public async Task <IActionResult> Details([FromQuery] string id)
        {
            try
            {
                var travelPlanId = new Guid(id);
                var userId       = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;

                var travelers = await _userTravelPlanRepository.GetTravelersForActivityAsync(travelPlanId);

                if (!travelers.Contains(new Guid(userId)))
                {
                    return(Forbid());
                }

                var userTravelers = await _userRepository.GetUsersAsync(travelers);

                var travelPlanDTO = await _travelPlanRepository.GetAsync(travelPlanId);

                travelPlanDTO.Travelers = userTravelers.ToList();

                return(Ok(travelPlanDTO));
            }
            catch
            {
                return(BadRequest());
            }
        }
Esempio n. 2
0
        public async Task <TPAnnouncementDto> CreateAsync(TPAnnouncementDto announcementDto, Guid loggedInUserId)
        {
            try
            {
                //validate logged in user is even able to make an announcement to travel plan
                var tpToAnnounceTo = await _travelPlanRepository.GetAsync(announcementDto.TravelPlanId);

                if (tpToAnnounceTo.CreatedById != loggedInUserId)
                {
                    throw new InsufficientRightsException("User doesn't have rights to delete");
                }

                var newAnnouncement = new TPAnnouncement
                {
                    Title                = announcementDto.Title,
                    Description          = announcementDto.Description,
                    CreatedDate          = DateTime.UtcNow,
                    CreatedById          = loggedInUserId,
                    TravelPlanId         = announcementDto.TravelPlanId,
                    TravelPlanActivityId = announcementDto.TravelPlanActivityId
                };

                _dbContext.TPAnnouncements.Add(newAnnouncement);

                var isSuccessful = await _dbContext.SaveChangesAsync() > 0;

                if (isSuccessful)
                {
                    return(announcementDto);
                }
                throw new CommonException("Problem occurred creating announcement");
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task <bool> DeleteAsync(Guid travelPlanId, Guid userId)
        {
            try
            {
                var travelPlanToDelete = await _travelPlanRepository.GetAsync(travelPlanId);

                if (travelPlanToDelete == null)
                {
                    return(true);
                }
                if (travelPlanToDelete.CreatedById != userId)
                {
                    throw new InsufficientRightsException("Insufficient rights to delete Travel Plan");
                }

                var isSuccessful = await _travelPlanRepository.DeleteAsync(travelPlanToDelete);

                return(isSuccessful);
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 4
0
        public async Task InviteUser(Guid inviter, string inviteeUsername, Guid travelPlanId)
        {
            try
            {
                var travelPlan = await _travelPlanRepository.GetAsync(travelPlanId, includeUTP : true);

                //validate the inviter is the host
                if (travelPlan.CreatedById != inviter)
                {
                    //log here
                    throw new InsufficientRightsException("User doesn't have rights to add to travelplan");
                }

                //validate invitee exists
                var userToInvite = await _userRepository.GetUserAsync(inviteeUsername);

                if (userToInvite == null)
                {
                    //log here
                    throw new UserNotFoundException("User to add does not exist");
                }

                //check if user to invite is already part of plan
                if (travelPlan.UserTravelPlans.Exists((utp) => utp.TravelPlanId == new Guid(userToInvite.Id)))
                {
                    throw new CommonException("User is already a traveler!");
                }

                var newInvitation = new PlanInvitation
                {
                    Created      = DateTime.UtcNow,
                    Expiration   = DateTime.UtcNow.AddDays(7),
                    InvitedById  = inviter,
                    InviteeId    = new Guid(userToInvite.Id),
                    TravelPlanId = travelPlanId
                };

                await _planInvitationRepository.InviteUser(newInvitation);
            }
            catch (Exception)
            {
                throw;
            }
        }