/// <summary> /// Validates the request to create a new trail /// </summary> public ValidationServiceResponse <TopoTrailInfo> ValidateCreate(ITopoTrailUpdateRequest request) { // load existing trail var response = new ValidationServiceResponse <TopoTrailInfo>(null); // do basic validation if (String.IsNullOrWhiteSpace(request.Name)) { response.AddError("Name", "Name is required!"); } // TODO: REFACTOR: use GeoPolitical var timezone = GeoTimezoneInfo.Find(request.Timezone); if (timezone == null) { response.AddError("Timezone", "Timezone is missing or invalid!"); } var country = GeoCountryInfo.Find(request.Country); if (country == null) { response.AddError("Country", "Country is missing or invalid!"); } var region = GeoRegionInfo.Find(request.Region); if (region == null && !String.IsNullOrWhiteSpace(request.Region)) { response.AddError("Region", "Region is invalid!"); } return(response); }
public TopoTrailInfo(ITopoTrailUpdateRequest data, List <GpxTrackData> tracks) { Name = data.Name; Description = data.Description; Keywords = data.Keywords; UrlText = data.UrlText; UrlLink = data.UrlLink; Timezone = GeoTimezoneInfo.Find(data.Timezone); if (Timezone == null) { Timezone = GeoTimezoneInfo.UTC; } Country = GeoCountryInfo.Find(data.Country); Region = GeoRegionInfo.Find(data.Region); Location = data.Location; if (tracks != null) { foreach (var track in tracks) { var t = new TopoTrackInfo(this, track); _tracks.Add(t); } } }
/// <summary> /// Validates the request to update an existing trail /// </summary> public ValidationServiceResponse <TopoTrailInfo> ValidateUpdate(ITopoTrailUpdateRequest request) { // validate basic request first var response = ValidateCreate(request); if (response.HasErrors) { return(response); } // load existing trail var trail = GetTrail(request.Key); if (trail == null) { response.AddError("Trail", $"Trail {request.Key} does not exist!"); } // fresh result with trail attached return(new ValidationServiceResponse <TopoTrailInfo>(trail)); }
/// <summary> /// Updates the trail metadata and synchronizes file and cache /// </summary> public ValidationServiceResponse <TopoTrailInfo> UpdateTrail(ITopoTrailUpdateRequest request) { // validate update request var response = ValidateUpdate(request); if (response.HasErrors) { return(response); } // process update request var trail = response.Data; // save current filename for later var existing = GetFilename(trail); // check file system if (!Directory.Exists(_rootUri)) { throw new Exception($"Directory not initalized: {_rootUri}"); } var folder = Path.Combine(_rootUri, trail.Country.Name); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } // update trail properties trail.Timezone = GeoTimezoneInfo.Find(request.Timezone); trail.Country = GeoCountryInfo.Find(request.Country); trail.Region = GeoRegionInfo.Find(request.Region); trail.Name = TextMutate.TrimSafe(request.Name); trail.Description = TextMutate.TrimSafe(request.Description); trail.Location = TextMutate.TrimSafe(request.Location); trail.Keywords = TextMutate.FixKeywords(request.Keywords); // TODO: BUG: fix url writing for v1.1 files! //trail.UrlLink = TextMutate.FixUrl(request.UrlLink); //trail.UrlText = TextMutate.TrimSafe(request.UrlText); // generate new and rename/remove existing files var filename = GetFilename(trail); // check if overwrite file var renamed = String.Compare(existing, filename, true) != 0; if (!renamed) { // temp rename current file File.Move(existing, existing + "~temp"); existing = existing + "~temp"; } // write new file var contents = BuildGpx(trail); File.WriteAllText(filename, contents); // delete old file File.Delete(existing); // refresh key and cache data if (renamed) { trail.Key = Path.GetFileNameWithoutExtension(filename).ToUpperInvariant(); _trails.Remove(request.Key.ToUpperInvariant()); _trails.Add(trail); } // return successful response return(response); }