Beispiel #1
0
        [Authorize(Roles = "Admin")] // 13. Part 9
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }

            TrailDto trailDto = await _trailRepo.GetAsync(_trailUrl, id.GetValueOrDefault(), HttpContext.Session.GetString("JWToken"));

            if (trailDto == null)
            {
                return(NotFound());
            }

            var nationalParkDtos = await _npRepo.GetAllSync(_npUrl, HttpContext.Session.GetString("JWToken"));

            ViewBag.NationalParksSelectList = new SelectList(nationalParkDtos, "Id", "Name", trailDto.NationalParkId);

            TrailUpdateDto trailUpdateDto = new TrailUpdateDto()
            {
                Id             = trailDto.Id,
                Name           = trailDto.Name,
                Distance       = trailDto.Distance,
                DifficultyType = trailDto.DifficultyType,
                NationalParkId = trailDto.NationalParkId
            };

            return(View(trailUpdateDto));
        }
Beispiel #2
0
        public async Task <TrailDto> GetAsync(string Url, int Id, string token)
        {
            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, Url + Id);
            HttpClient         httpClient  = _httpClientFactory.CreateClient();

            // 13. Part 6
            // ----------------------
            if (token != null && token.Length > 0)
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            }
            // ----------------------

            HttpResponseMessage httpResponse = await httpClient.SendAsync(httpRequest);

            if (httpResponse.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var jsonString = await httpResponse.Content.ReadAsStringAsync();

                TrailDto trailDto = JsonConvert.DeserializeObject <TrailDto>(jsonString);

                return(trailDto);
            }

            return(null);
        }
Beispiel #3
0
        public CreateTrailScreenViewModel(MainContext context, RideDto ride) : base(context)
        {
            trail       = new TrailDto();
            Ride        = ride;
            count       = 1;
            displayText = "Tap on the map to set a start point";

            MapTapped += MapViewModel_MapTapped;
        }
Beispiel #4
0
        public IActionResult GetTrail(int TrailId)
        {
            Trail Trail = _trailRepo.GetTrail(TrailId);

            if (Trail == null)
            {
                return(NotFound());
            }
            TrailDto TrailDto = _mapper.Map <TrailDto>(Trail);

            return(Ok(TrailDto));
        }
Beispiel #5
0
        public async Task <bool> Load(int id)
        {
            try {
                Trail = await Context.Services.GetTrail(id);

                OnPropertyChanged();

                return(true);
            } catch (ServiceException ex) {
                Toast.LongAlert(ex.Message);

                return(false);
            }
        }
Beispiel #6
0
        public async Task <IActionResult> PostTrail(TrailDto trail)
        {
            try
            {
                var newTrail = await repository.Add(trail);

                return(Ok());
            }
            catch (ArgumentNullException e)
            {
                var paramName = e.ParamName;

                if (paramName != null)
                {
                    return(NotFound(paramName));
                }

                throw;
            }
        }
Beispiel #7
0
        public ActionResult <int> Add(TrailDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!this.IsCurrentUserAdmin())
            {
                return(NotFound());
            }

            int userId = this.GetCurrentUserId();

            int trailId = SaveTrail(userId, model);

            new TrailAnalyser().AnalyseTrail(context, userId, trailId);

            return(trailId);
        }
Beispiel #8
0
        private int SaveTrail(int userId, TrailDto model)
        {
            Trail trail = new Trail {
                Name   = model.Name,
                UserId = userId,
            };

            trail.TrailLocations = model.Locations
                                   .Select(i => new TrailLocation {
                Latitude  = i.Latitude,
                Longitude = i.Longitude,
                Order     = i.Order,
            })
                                   .ToList();

            context.Trails.Add(trail);

            context.SaveChanges();

            return(trail.TrailId);
        }
Beispiel #9
0
        public async Task <Trail> Add(TrailDto newTrail)
        {
            var startPoint    = new Location(newTrail.StartPoint.Longitude, newTrail.StartPoint.Latitude);
            var endPoint      = new Location(newTrail.EndPoint.Longitude, newTrail.EndPoint.Latitude);
            var newStartPoint = context.Add(startPoint);
            var newEndPoint   = context.Add(endPoint);

            IEnumerable <Location> checkpoints = new List <Location>();

            if (newTrail.Checkpoints != null)
            {
                foreach (var item in newTrail.Checkpoints)
                {
                    checkpoints.Append(item);
                }
            }
            var newCheckPoint = context.Add(checkpoints);
            var trail         = new Trail(newStartPoint.Entity, newEndPoint.Entity, newCheckPoint.Entity);

            context.Add(trail);
            await context.SaveChangesAsync();

            return(trail);
        }
Beispiel #10
0
 public async Task <int> UploadTrail(TrailDto trail)
 {
     return(await PostAsync <int>("trails/add", trail));
 }