public async Task <SheetModel> Update(SheetModel model) { using (var db = new ScribsDbContext()) { var user = GetUser(db); var sheet = await Sheet.Factory.GetInstanceAsync(db, model.Id); if (sheet.Name != model.Name) { sheet.Name = model.Name; } var kept = new HashSet <int>(); foreach (var fieldModel in model.Fields.Where(o => o.Id > 0)) { kept.Add(fieldModel.Id); var field = sheet.SheetFields.Single(o => o.Id == fieldModel.Id); field.Value = fieldModel.Value; } foreach (var fieldModel in model.Fields.Where(o => o.Id == 0)) { CreateField(db, sheet, fieldModel); } foreach (var field in sheet.SheetFields.Where(o => !kept.Contains(o.Id))) { sheet.SheetFields.Remove(field); SheetField.Factory.Delete(db, field); } await db.SaveChangesAsync(); return(SheetModelUtils.GetModel(sheet)); } }
public async Task <SheetTemplateModel> Post(SheetTemplateModel model) { using (var db = new ScribsDbContext()) { var user = GetUser(db); var template = SheetTemplate.Factory.CreateInstance(db); template.ProjectKey = model.ProjectKey; template.Name = model.Name; foreach (var fieldModel in model.Fields.Values) { CreateField(db, template, fieldModel); } await db.SaveChangesAsync(); return(SheetModelUtils.GetModel(template, true)); } }
public async Task <SheetModel> Post(SheetModel model) { using (var db = new ScribsDbContext()) { var user = GetUser(db); var sheet = Sheet.Factory.CreateInstance(db); sheet.ProjectKey = model.ProjectKey; sheet.Name = model.Name; sheet.SheetTemplateId = model.SheetTemplateId; foreach (var fieldModel in model.Fields) { CreateField(db, sheet, fieldModel); } await db.SaveChangesAsync(); return(SheetModelUtils.GetModel(sheet)); } }
public async Task <SheetTemplateModel> Update(SheetTemplateModel model) { using (var db = new ScribsDbContext()) { var user = GetUser(db); var template = await SheetTemplate.Factory.GetInstanceAsync(db, model.Id); if (template.Name != model.Name) { template.Name = model.Name; } var kept = new HashSet <int>(); foreach (var kvp in model.Fields.Where(o => o.Key > 0)) { kept.Add(kvp.Key); var field = template.SheetTemplateFields.Single(o => o.Id == kvp.Key); field.Label = kvp.Value.Label; field.Index = kvp.Value.Index; } foreach (var fieldModel in model.Fields.Where(o => o.Key == 0).Select(o => o.Value)) { CreateField(db, template, fieldModel); } foreach (var field in template.SheetTemplateFields.Where(o => !kept.Contains(o.Id))) { foreach (var sheet in template.Sheets) { var sheetField = sheet.SheetFields.SingleOrDefault(o => o.SheetTemplateFieldId == field.Id); sheet.SheetFields.Remove(sheetField); SheetField.Factory.Delete(db, sheetField); } template.SheetTemplateFields.Remove(field); SheetTemplateField.Factory.Delete(db, field); } await db.SaveChangesAsync(); return(SheetModelUtils.GetModel(template, true)); } }
public async Task <ProjectModel> Post(ProjectModel model) { using (var db = new ScribsDbContext()) { var user = GetUser(db); int index = user.GetProjects().Count(); var project = new Project(user, model.Name, false); if (project.ExistsItem()) { throw new System.Exception("This project already exists"); } project.CreateItem(); project.Load(); project.Type = (Project.ProjectTypes)model.Type; project.Description = model.Description; project.Structure = model.Structure.Aggregate((a, b) => a + ";" + b); project.Index = 0; project.Key = Guid.NewGuid().ToString(); // Basic sheet templates var charTemplate = SheetTemplate.Factory.CreateInstance(db); charTemplate.User = user; charTemplate.ProjectKey = project.Key; charTemplate.Name = "Characters"; int i = 0; foreach (string label in new List <string> { "Name", "Description", "Personality", "Occupation", "Objective", "Habits", "Conflicts", "Relatives", "Notes" }) { var field = SheetTemplateField.Factory.CreateInstance(db); field.Index = i++; field.Label = label; field.SheetTemplate = charTemplate; charTemplate.SheetTemplateFields.Add(field); } var setTemplate = SheetTemplate.Factory.CreateInstance(db); setTemplate.User = user; setTemplate.ProjectKey = project.Key; setTemplate.Name = "Settings"; i = 0; foreach (string label in new List <string> { "Name", "Description", "Sights", "Sounds", "Smells", "Notes" }) { var field = SheetTemplateField.Factory.CreateInstance(db); field.Index = i++; field.Label = label; field.SheetTemplate = setTemplate; setTemplate.SheetTemplateFields.Add(field); } await db.SaveChangesAsync(); // Drafts var draftDirectory = FileSystemItem.Create(project, Directory.Type, ".Drafts", Guid.NewGuid().ToString(), 0) as Directory; FileSystemItem.Create(draftDirectory, File.Type, "Draft 1", Guid.NewGuid().ToString(), 0); // Structure generation var folders = model.Structure.Length > 1 ? model.Structure.Take(model.Structure.Length - 1) : new List <string> { "folder" }; var file = model.Structure.Length > 0 ? model.Structure.Last() : "file"; var directory = project as Directory; foreach (string folder in folders) { string directoryName = folder.Substring(0, 1).ToUpper() + folder.Substring(1, folder.Length - 1) + " 1"; directory = FileSystemItem.Create(directory, Directory.Type, directoryName, Guid.NewGuid().ToString(), 0) as Directory; } string fileName = file.Substring(0, 1).ToUpper() + file.Substring(1, file.Length - 1) + " 1"; FileSystemItem.Create(directory, File.Type, fileName, Guid.NewGuid().ToString(), 0); project.Save(); return(new ProjectModel { Name = model.Name, Discriminator = Discriminator.Directory, Key = project.Key }); } }