public async Task OnGetAsync(long?Id) { var configuration = new DataAccess.Models.Configuration(); if (Id != null) { configuration = await dbContext.Configurations.Where(o => o.Id == Id).Include(x => x.ConfigurationDict).FirstOrDefaultAsync(); if (configuration == null) { Message = "There is no such configuration"; } await SetPageModel(configuration); } else { Configuration = new ConfigurationVM(); var userId = User.GetLoggedInUserId <int>(); var organization = await layoutService.GetOrganizationByUserId(userId); if (organization != null) { Input = new InputModel() { OrganizationId = organization.Id }; } Organizations = new SelectList(await dbContext.Organizations.Select(x => x).ToListAsync(), nameof(DataAccess.Models.Organization.Id), nameof(DataAccess.Models.Organization.DisplayName)); } }
public async Task <IActionResult> OnPostAsync() { DataAccess.Models.Configuration configuration = null; var userId = User.GetLoggedInUserId <int>(); var user = await dbContext.Users.SingleOrDefaultAsync(x => x.Id == userId); configuration = Input.Id == 0 ? new DataAccess.Models.Configuration() : await dbContext.Configurations.Where(o => o.Id == Input.Id).Include(x => x.ConfigurationDict).FirstOrDefaultAsync(); if (ModelState.IsValid) { configuration.Value = Input.Value; configuration.Notes = Input.Notes; if (Input.Id == 0) { configuration.CreateAt = DateTime.UtcNow; configuration.CreateBy = userId; configuration.OrganizationId = user.OrganizationId ?? Input.OrganizationId; if (Input.ConfigurationDictId == 0) { configuration.ConfigurationDict = new ConfigurationDict() { CreateAt = DateTime.UtcNow, CreateBy = userId, Notes = Input.Notes, IsActive = true, IsEncrypted = false, Key = Input.Key, DefaultValue = Input.Value }; } dbContext.Configurations.Add(configuration); } else { configuration.UpdateAt = DateTime.UtcNow; configuration.UpdateBy = userId; configuration.ConfigurationDict.Key = Input.Key; configuration.ConfigurationDict.UpdateAt = DateTime.UtcNow; configuration.ConfigurationDict.UpdateBy = userId; } await dbContext.SaveChangesAsync(); return(LocalRedirect("/Configuration/Index")); } else { await SetPageModel(configuration); } return(Page()); }
private async Task SetPageModel(DataAccess.Models.Configuration configuration) { Organizations = new SelectList(await dbContext.Organizations.Select(x => x).ToListAsync(), nameof(DataAccess.Models.Organization.Id), nameof(DataAccess.Models.Organization.DisplayName)); Configuration = new ConfigurationVM() { Id = configuration.Id, Key = configuration.ConfigurationDict.Key, Value = configuration.Value, Notes = configuration.Notes, ConfigurationDictId = configuration.ConfigurationDictId, OrganizationId = configuration.OrganizationId }; Input = new InputModel() { Id = configuration.Id, Key = configuration.ConfigurationDict.Key, Value = configuration.Value, Notes = configuration.Notes, ConfigurationDictId = configuration.ConfigurationDictId, OrganizationId = configuration.OrganizationId }; }