public async Task <IActionResult> Create([FromBody] TravelPlanActivityDto activityDto)
        {
            try
            {
                var userId      = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
                var newActivity = await _activityService.CreateAsync(activityDto, new Guid(userId));

                return(Ok(newActivity));
            }
            catch (Exception exc)
            {
                return(BadRequest());
            }
        }
        public async Task <TravelPlanActivityDto> EditAsync(TravelPlanActivityDto activityDto, Guid userId)
        {
            try
            {
                var activityToEdit = await _dbContext.TravelPlanActivities
                                     .Include(tpa => tpa.Location)
                                     .FirstOrDefaultAsync(tpa => tpa.TravelPlanActivityId == activityDto.Id);

                if (activityToEdit == null)
                {
                    throw new Exception("Activity not found");
                }
                if (activityToEdit.HostId != userId)
                {
                    throw new InsufficientRightsException("Insufficient rights to edit activity");
                }

                //map lib here
                activityToEdit.Name               = activityDto.Name;
                activityToEdit.StartTime          = activityDto.StartTime;
                activityToEdit.EndTime            = activityDto.EndTime;
                activityToEdit.Location.Address   = activityDto.Location.Address;
                activityToEdit.Location.Longitude = activityDto.Location.Longitude;
                activityToEdit.Location.Latitude  = activityDto.Location.Latitude;
                activityToEdit.Category           = activityDto.Category;

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

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

                if (isSuccessful)
                {
                    return(new TravelPlanActivityDto(activityToEdit));
                }
                throw new Exception("Error saving changes");
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task <TravelPlanActivityDto> CreateAsync(TravelPlanActivityDto activityDto, Guid userId)
        {
            try
            {
                //check if TravelPlan exists
                var travelPlan = await _dbContext.TravelPlans.FindAsync(activityDto.TravelPlanId);

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

                //map here
                var newActivity = new TravelPlanActivity
                {
                    Name      = activityDto.Name,
                    StartTime = activityDto.StartTime,
                    EndTime   = activityDto.EndTime,
                    Category  = activityDto.Category,
                    Location  = new Location
                    {
                        Address   = activityDto.Location.Address,
                        Latitude  = activityDto.Location.Latitude,
                        Longitude = activityDto.Location.Longitude,
                    },
                    HostId       = userId,
                    TravelPlanId = activityDto.TravelPlanId
                };

                _dbContext.TravelPlanActivities.Add(newActivity);

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

                if (isSuccessful)
                {
                    return(new TravelPlanActivityDto(newActivity));
                }
                throw new Exception("Error saving changes");
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task <IActionResult> Edit([FromBody] TravelPlanActivityDto activityDto)
        {
            try
            {
                var userId            = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
                var editedActivityDto = await _activityService.EditAsync(activityDto, new Guid(userId));

                return(Ok(editedActivityDto));
            }
            catch (InsufficientRightsException insufRights)
            {
                return(BadRequest(new
                {
                    message = insufRights.Message
                }));
            }
            catch (Exception exc)
            {
                return(BadRequest());
            }
        }
Esempio n. 5
0
        public async Task <TravelPlanActivityDto> EditAsync(TravelPlanActivityDto activityDto, Guid userId)
        {
            try
            {
                var activityToEdit = await _travelPlanActivityRepository.GetAsync(activityDto.Id);

                if (activityToEdit == null)
                {
                    throw new Exception("Activity not found");
                }
                if (activityToEdit.HostId != userId)
                {
                    throw new InsufficientRightsException("Insufficient rights to edit activity");
                }

                //map lib here
                activityToEdit.Name               = activityDto.Name;
                activityToEdit.StartTime          = activityDto.StartTime;
                activityToEdit.EndTime            = activityDto.EndTime;
                activityToEdit.Location.Address   = activityDto.Location.Address;
                activityToEdit.Location.Longitude = activityDto.Location.Longitude;
                activityToEdit.Location.Latitude  = activityDto.Location.Latitude;
                activityToEdit.Category           = activityDto.Category;

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

                if (isSuccessful)
                {
                    return(new TravelPlanActivityDto(activityToEdit));
                }
                throw new Exception("Error saving changes");
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 6
0
        public async Task <TravelPlanActivityDto> CreateAsync(TravelPlanActivityDto activityDto, Guid userId)
        {
            try
            {
                var travelPlan = await _travelPlanService.GetAsync(activityDto.TravelPlanId);

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

                //map here
                var newActivity = new TravelPlanActivity
                {
                    Name      = activityDto.Name,
                    StartTime = activityDto.StartTime,
                    EndTime   = activityDto.EndTime,
                    Category  = activityDto.Category,
                    Location  = new Location
                    {
                        Address   = activityDto.Location.Address,
                        Latitude  = activityDto.Location.Latitude,
                        Longitude = activityDto.Location.Longitude,
                    },
                    HostId       = userId,
                    TravelPlanId = activityDto.TravelPlanId
                };

                var addedActivity = await _travelPlanActivityRepository.CreateAsync(newActivity);

                return(new TravelPlanActivityDto(addedActivity));
            }
            catch (Exception)
            {
                throw;
            }
        }