public ActivePermitView(Database database, NancyContext context) { Permits = database.FindPermitsForUser(context.CurrentUser) .Select(x => new PermitApplicationContainer { Id = x.Id, Status = PermitStatus.Text((int)x.Status), Permit = (PermitApplicationRecord)Serializer.Deserialize(new StringReader(x.Data), typeof(PermitApplicationRecord)) }) .ToList(); }
private Response UpdateApplication(Database database, HostConfigurationSection config, AuthUser user, int id) { // Configuration parameters var workspace = config.Keyword(Keys.UPP__PERMIT_WORKSPACE); var repoUrlTemplate = config.Keyword(Keys.UPP__PERMIT_REPOSITORY_URL_TEMPLATE); var issuerId = config.Keyword(Keys.SELF__IDENTIFIER); // Find the record var bundle = database.ApplicationById(id); // Generate the repository path var repoPath = Path.Combine(workspace, bundle.Application.Id); // If the repository does not already exists, clone it if (!Directory.Exists(repoPath)) { Repository.Clone(bundle.Application.Links.Origin, repoPath); } JObject permit = null; string path; using (var repo = new Repository(repoPath)) { // Update the checkout var author = new Signature(user.UserName, user.Email, DateTime.Now); var mergeResult = Commands.Pull(repo, author, new PullOptions()); // Read the current permit file and return path = Path.Combine(repo.Info.WorkingDirectory, "permit.json"); permit = JObject.Parse(File.ReadAllText(path)); } // Convert the form into a PermitApplicationRecord var permitForm = Context.ToPermitDataRecord(); var authorities = permit.SelectToken("data.attributes.authorities"); var mySection = authorities.SelectToken(issuerId); var formSection = permit.SelectToken("data.attributes.form-data"); if (mySection == null) { (permit.SelectToken("data.attributes.authorities") as JObject).Add(issuerId, new JObject()); mySection = authorities.SelectToken(issuerId); } permit.SelectToken("data.attributes")["route"] = (JObject.Parse(permitForm.Route)); mySection["route"] = JObject.Parse(permitForm.Route); mySection["status"] = PermitStatus.Text(int.Parse(permitForm.Status)); mySection["reviewed"] = DateTime.Now; formSection["movementInfo.startDate"] = permitForm.Movement.StartDate; formSection["movementInfo.endDate"] = permitForm.Movement.EndDate; formSection["movementInfo.haulingHours"] = permitForm.Movement.HaulingHours; formSection["movementInfo.origin"] = permitForm.Movement.Origin; formSection["movementInfo.destination"] = permitForm.Movement.Destination; formSection["movementInfo.routeDescription"] = permitForm.Movement.RouteDescription; formSection["movementInfo.routeCountyNumbers"] = permitForm.Movement.RouteCountyNumbers; formSection["movementInfo.milesOfCountyRoad"] = permitForm.Movement.MilesOfCountyRoad; formSection["movementInfo.routeLength"] = permitForm.Movement.RouteLength; formSection["movementInfo.stateHighwayPermitNumber"] = permitForm.Movement.StateHighwayPermitNumber; formSection["movementInfo.stateHighwayPermitIssued"] = permitForm.Movement.StateHighwayPermitIssued; // Serialize all of the information into a JSON string var json = permit.ToJson(); // Serialize the json content back to the file File.WriteAllText(path, JsonConvert.SerializeObject(permit, Formatting.Indented)); // Commit the changes using (var repo = new Repository(repoPath)) { Commands.Stage(repo, path); var author = new Signature(user.UserName, user.Email, DateTime.Now); var committer = author; // Commit the files to the and push to the origin var commit = repo.Commit("Update the permit form data", author, committer); var options = new PushOptions(); repo.Network.Push(repo.Branches["master"], options); } // Add the permit to the database database.UpdateApplication(json, id, Context.Request.Form["status"]); return(new Nancy.Responses.RedirectResponse("../../../../permits/")); }