public async Task <TravelPlanDto> CreateAsync(TravelPlanDto travelPlanDto, Guid userId)
        {
            try
            {
                //map here

                var newTravelPlan = new Domain.Models.TravelPlan
                {
                    Name               = travelPlanDto.Name,
                    Description        = travelPlanDto.Description,
                    StartDate          = travelPlanDto.StartDate,
                    EndDate            = travelPlanDto.EndDate,
                    CreatedById        = userId,
                    TravelPlanStatusId = (int)TravelPlanStatusEnum.Created,
                    //add jxn, auto adds to UTP table
                    UserTravelPlans = new List <UserTravelPlan>
                    {
                        new UserTravelPlan
                        {
                            UserId = userId
                        }
                    }
                };

                var tp = await _travelPlanRepository.CreateAsync(newTravelPlan);

                return(new TravelPlanDto(tp));
            }
            catch (Exception)
            {
                throw;
            }
        }
        //public async Task<TravelPlanDto> EditAsync(TravelPlanDto travelPlanDto, Guid userId)
        //{
        //    try
        //    {
        //        var travelPlanToEdit = await _dbContext.TravelPlans.FindAsync(travelPlanDto.Id);
        //        if (travelPlanToEdit == null) throw new Exception("Travel Plan Not Found");

        //        if (travelPlanToEdit.CreatedById != userId) throw new InsufficientRightsException("Insufficient rights to edit Travel Plan");

        //        //map here
        //        travelPlanToEdit.TravelPlanId = travelPlanDto.Id;
        //        travelPlanToEdit.Name = travelPlanDto.Name;
        //        travelPlanToEdit.StartDate = travelPlanDto.StartDate;
        //        travelPlanToEdit.EndDate = travelPlanDto.EndDate;
        //        travelPlanToEdit.Description = travelPlanDto.Description;

        //        if (!_dbContext.ChangeTracker.HasChanges()) return travelPlanDto;

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

        //        if (isSuccessful)
        //        {
        //            return new TravelPlanDto(travelPlanToEdit);
        //        }
        //        throw new Exception("Problem Editing Travel Plan");
        //    }
        //    catch (Exception)
        //    {
        //        throw;
        //    }
        //}

        //public async Task<bool> UpdateTPStatus(TravelPlan travelPlanToEdit, int status)
        //{
        //    if (!_dbContext.ChangeTracker.HasChanges())
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        var isSuccessful = await _dbContext.SaveChangesAsync() > 0;
        //        return isSuccessful;
        //    }
        //}


        public async Task <TravelPlan> GetAsync(Guid travelPlanId, bool includeUTP = false)
        {
            try
            {
                TravelPlan travelPlan;
                if (includeUTP)
                {
                    travelPlan = await _dbContext.TravelPlans.Where((tp) => tp.TravelPlanId == travelPlanId)
                                 .Include(tp => tp.UserTravelPlans)
                                 .FirstOrDefaultAsync();
                }
                else
                {
                    travelPlan = await _dbContext.TravelPlans.FindAsync(travelPlanId);
                }


                var travelPlanDto = new TravelPlanDto(travelPlan);
                var tpStatus      = await _dbContext.TravelPlanStatuses.Where(tps => tps.UniqStatus == (TravelPlanStatusEnum)travelPlan.TravelPlanStatusId)
                                    .FirstOrDefaultAsync();

                travelPlanDto.TravelPlanStatus = new TravelPlanStatusDto
                {
                    UniqStatus  = tpStatus.UniqStatus,
                    Description = tpStatus.Description
                };

                return(travelPlan);
            }
            catch
            {
                throw;
            }
        }
        public async Task <List <TravelPlanDto> > ListAsync(Guid userId, int?status = null)
        {
            try
            {
                //get travel plans associated with the user, whether they created it or just belong it
                var userTravelPlanIds = await _userTravelPlanService.GetUserTravelPlanIDsAsync(userId);

                var travelPlans = await _travelPlanRepository.GetTravelPlansWithFilterAsync(userTravelPlanIds, status);

                List <TravelPlanDto> lstTravelPlanDto = new List <TravelPlanDto>();

                foreach (var tp in travelPlans)
                {
                    var tpDto    = new TravelPlanDto(tp);
                    var tpStatus = await _travelPlanStatusService.GetStatusAsync((TravelPlanStatusEnum)tp.TravelPlanStatusId);

                    tpDto.TravelPlanStatus = new TravelPlanStatusDto
                    {
                        UniqStatus  = tpStatus.UniqStatus,
                        Description = tpStatus.Description
                    };

                    lstTravelPlanDto.Add(tpDto);
                }

                return(lstTravelPlanDto);
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #4
0
        public async Task <TravelPlanDto> GetAsync(Guid travelPlanId)
        {
            try
            {
                var travelPlan = await _dbContext.TravelPlans.FindAsync(travelPlanId);

                if (travelPlan == null)
                {
                    throw new Exception("Travel Plan not found");
                }

                var travelPlanDto = new TravelPlanDto(travelPlan);
                var tpStatus      = await _dbContext.TravelPlanStatuses.Where(tps => tps.UniqStatus == (TravelPlanStatusEnum)travelPlan.TravelPlanStatusId).FirstOrDefaultAsync();

                travelPlanDto.TravelPlanStatus = new TravelPlanStatusDto
                {
                    UniqStatus  = tpStatus.UniqStatus,
                    Description = tpStatus.Description
                };

                return(travelPlanDto);
            }
            catch
            {
                throw;
            }
        }
Beispiel #5
0
        public async Task <IActionResult> Create([FromBody] TravelPlanDto travelPlanDto)
        {
            try
            {
                var userId        = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
                var newTravelPlan = await _travelPlanRepository.CreateAsync(travelPlanDto, new Guid(userId));

                return(Ok(newTravelPlan));
            }
            catch (Exception exc)
            {
                return(BadRequest());
            }
        }
Beispiel #6
0
        public async Task <List <TravelPlanDto> > ListAsync(Guid userId, int?status = null)
        {
            try
            {
                //get travel plans associated with the user, whether they created it or just belong it
                var userTravelPlanIds = await _dbContext.UserTravelPlans.Where(utp => utp.UserId == userId).Select((utp) => utp.TravelPlanId).ToListAsync();

                var travelPlans = new List <TravelPlan>();

                //if null, aka not specified get all,
                //else get specific
                if (status == null)
                {
                    travelPlans = await _dbContext.TravelPlans.Where((tp) => userTravelPlanIds.Contains(tp.TravelPlanId)).ToListAsync();
                }
                else if (Enum.IsDefined(typeof(TravelPlanStatusEnum), status))
                {
                    travelPlans = await _dbContext.TravelPlans.Where((tp) => userTravelPlanIds.Contains(tp.TravelPlanId) && tp.TravelPlanStatusId == status).ToListAsync();
                }

                List <TravelPlanDto> lstTravelPlanDto = new List <TravelPlanDto>();

                foreach (var tp in travelPlans)
                {
                    var tpDto    = new TravelPlanDto(tp);
                    var tpStatus = await _dbContext.TravelPlanStatuses.Where(tps => tps.UniqStatus == (TravelPlanStatusEnum)tp.TravelPlanStatusId).FirstOrDefaultAsync();

                    tpDto.TravelPlanStatus = new TravelPlanStatusDto
                    {
                        UniqStatus  = tpStatus.UniqStatus,
                        Description = tpStatus.Description
                    };

                    lstTravelPlanDto.Add(tpDto);
                }

                return(lstTravelPlanDto);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task <TravelPlanDto> EditAsync(TravelPlanDto travelPlanDto, Guid userId)
        {
            try
            {
                var travelPlanToEdit = await _travelPlanRepository.GetAsync(travelPlanDto.Id);

                if (travelPlanToEdit == null)
                {
                    throw new Exception("Travel Plan Not Found");
                }

                if (travelPlanToEdit.CreatedById != userId)
                {
                    throw new InsufficientRightsException("Insufficient rights to edit Travel Plan");
                }

                //map here
                travelPlanToEdit.TravelPlanId = travelPlanDto.Id;
                travelPlanToEdit.Name         = travelPlanDto.Name;
                travelPlanToEdit.StartDate    = travelPlanDto.StartDate;
                travelPlanToEdit.EndDate      = travelPlanDto.EndDate;
                travelPlanToEdit.Description  = travelPlanDto.Description;

                if (!_dbContext.ChangeTracker.HasChanges())
                {
                    return(travelPlanDto);
                }

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

                if (isSuccessful)
                {
                    return(new TravelPlanDto(travelPlanToEdit));
                }
                throw new Exception("Problem Editing Travel Plan");
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #8
0
        public async Task <IActionResult> Edit([FromBody] TravelPlanDto travelPlanDto)
        {
            try
            {
                var userId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
                var editedTravelPlanDto = await _travelPlanRepository.EditAsync(travelPlanDto, new Guid(userId));

                return(Ok(editedTravelPlanDto));
            }
            catch (InsufficientRightsException insufRights)
            {
                return(BadRequest(new
                {
                    Message = insufRights.Message
                }));
            }
            catch (Exception exc)
            {
                return(BadRequest());
            }
        }
Beispiel #9
0
        public async Task <TravelPlanDto> CreateAsync(TravelPlanDto travelPlanDto, Guid userId)
        {
            try
            {
                //map here
                var newTravelPlan = new TravelPlan
                {
                    Name               = travelPlanDto.Name,
                    Description        = travelPlanDto.Description,
                    StartDate          = travelPlanDto.StartDate,
                    EndDate            = travelPlanDto.EndDate,
                    CreatedById        = userId,
                    TravelPlanStatusId = (int)TravelPlanStatusEnum.Created
                };

                await _dbContext.TravelPlans.AddAsync(newTravelPlan);

                //add to jxn table
                var traveler = new UserTravelPlan
                {
                    TravelPlan = newTravelPlan,
                    UserId     = userId
                };

                await _dbContext.UserTravelPlans.AddAsync(traveler);

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

                if (isSuccessful)
                {
                    return(new TravelPlanDto(newTravelPlan));
                }
                throw new Exception("Problem Editing Travel Plan");
            }
            catch (Exception exc)
            {
                throw;
            }
        }
        public async Task <TravelPlanDto> GetAsync(Guid travelPlanId, bool includeUTP = false, bool includeStatus = false)
        {
            var travelPlan = await _travelPlanRepository.GetAsync(travelPlanId, includeUTP);

            if (travelPlan == null)
            {
                throw new Exception("Travel Plan not found");
            }

            var travelPlanDto = new TravelPlanDto(travelPlan);

            if (includeStatus)
            {
                var tpStatus = await _travelPlanStatusService.GetStatusAsync((TravelPlanStatusEnum)travelPlan.TravelPlanStatusId);

                travelPlanDto.TravelPlanStatus = new TravelPlanStatusDto
                {
                    UniqStatus  = tpStatus.UniqStatus,
                    Description = tpStatus.Description
                };
            }

            return(travelPlanDto);
        }