public async Task <ActionResult> Create(ContactGroupViewModel model) { if (!ModelState.IsValid) { return(View(model)); } IEnumerable <ContactGroup> records = (await databaseApi.CreateRecordsAsync("contact_group", new List <ContactGroup> { model.ContactGroup }, new SqlQuery())).Records; List <ContactContactGroup> relationshipModel = model.Contacts .Where(x => x.InGroup) .Select(x => new ContactContactGroup { ContactId = x.ContactId, ContactGroupId = records.Select(y => y.Id).FirstOrDefault() }) .ToList(); if (relationshipModel.Count > 0) { await databaseApi.CreateRecordsAsync("contact_group_relationship", relationshipModel, new SqlQuery()); } return(RedirectToAction("List")); }
public async Task RunAsync(IRestContext context) { // Getting database interface IDatabaseApi databaseApi = context.Factory.CreateDatabaseApi("db"); // List available tables List <string> tables = new List <string>(await databaseApi.GetTableNamesAsync()); Console.WriteLine("Existing tables: {0}", tables.ToStringList()); // Delete staff table if it exists if (tables.Contains(TableName)) { Console.WriteLine("Deleting table {0}...", TableName); if (await databaseApi.DeleteTableAsync(TableName)) { Console.WriteLine("Table deleted."); } } Console.WriteLine("Creating {0} table schema...", TableName); TableSchema staffTableSchema = CreateTestTableSchema(); await databaseApi.CreateTableAsync(staffTableSchema); // Describe table staffTableSchema = await databaseApi.DescribeTableAsync(TableName); Console.WriteLine("Got {0} table schema, table's label is {1}", TableName, staffTableSchema.label); // Create new record Console.WriteLine("Creating {0} records...", TableName); List <StaffRecord> records = CreateStaffRecords().ToList(); records = new List <StaffRecord>(await databaseApi.CreateRecordsAsync(TableName, records, new SqlQuery())); SqlQuery query = new SqlQuery { filter = "age > 30", order = "age", fields = "*" }; var selection = await databaseApi.GetRecordsAsync <StaffRecord>(TableName, query); var ages = selection.Select(x => x.age.ToString(CultureInfo.InvariantCulture)).ToStringList(); Console.WriteLine("Get records with SqlQuery: ages={0}", ages); // Deleting one record Console.WriteLine("Deleting second record..."); await databaseApi.DeleteRecordsAsync(TableName, records.Skip(1).Take(1)); // Get table records records = new List <StaffRecord>(await databaseApi.GetRecordsAsync <StaffRecord>(TableName, new SqlQuery())); Console.WriteLine("Retrieved {0} records:", TableName); foreach (StaffRecord item in records) { Console.WriteLine("\t{0}", item); } }
public void ShouldCreateRecordsAsync() { // Arrange IDatabaseApi databaseApi = CreateDatabaseApi(); IEnumerable <StaffRecord> records = CreateStaffRecords().ToList(); SqlQuery query = new SqlQuery { Fields = "*" }; // Act List <StaffRecord> created = databaseApi.CreateRecordsAsync("staff", records, query).Result.Records; // Assert created.Count.ShouldBe(3); created.First().Uid.ShouldBe(1); created.Last().Uid.ShouldBe(3); Should.Throw <ArgumentNullException>(() => databaseApi.CreateRecordsAsync(null, records, query)); Should.Throw <ArgumentNullException>(() => databaseApi.CreateRecordsAsync("staff", (List <StaffRecord>)null, query)); Should.Throw <ArgumentNullException>(() => databaseApi.CreateRecordsAsync("staff", records, null)); }
public async Task <ActionResult> Create(ContactInfoViewModel model) { model.ContactInfo.InfoType = model.InfoType.ToString(); if (!ModelState.IsValid) { return(View(model)); } await databaseApi.CreateRecordsAsync("contact_info", new List <ContactInfo> { model.ContactInfo }, new SqlQuery()); return(Redirect(model.ReturnUrl)); }
public void ShouldCreateRecordsAsync() { // Arrange IDatabaseApi databaseApi = CreateDatabaseApi(); IEnumerable <StaffRecord> records = CreateStaffRecords(); SqlQuery query = new SqlQuery { fields = "*" }; // Act List <StaffRecord> created = databaseApi.CreateRecordsAsync("staff", records, query).Result.ToList(); // Assert created.Count.ShouldBe(3); created.First().uid.ShouldBe(1); created.Last().uid.ShouldBe(3); }
public async Task <ActionResult> Create(ContactCreateViewModel model) { if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0 && !validImageTypes.Contains(model.ImageUpload.ContentType)) { ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image."); } if (!ModelState.IsValid) { return(View(model)); } string result = await UploadImage(model.ImageUpload); if (result != null) { model.Contact.ImageUrl = DreamFactoryContext.BaseAddress + "/files/" + result; } IEnumerable <Contact> records = new List <Contact> { model.Contact }; records = (await databaseApi.CreateRecordsAsync("contact", records, new SqlQuery())).Records; IEnumerable <ContactContactGroup> relationshipRecords = new List <ContactContactGroup> { new ContactContactGroup { ContactId = records.Select(x => x.Id).FirstOrDefault(), ContactGroupId = model.GroupId } }; model.ContactInfoViewModel.ContactInfo.InfoType = model.ContactInfoViewModel.InfoType.ToString(); model.ContactInfoViewModel.ContactInfo.ContactId = records.Select(x => x.Id).FirstOrDefault(); Task <DatabaseResourceWrapper <ContactContactGroup> > createRelationshipsTask = databaseApi.CreateRecordsAsync("contact_group_relationship", relationshipRecords, new SqlQuery()); Task <DatabaseResourceWrapper <ContactInfo> > createContactInfoTask = databaseApi.CreateRecordsAsync("contact_info", new List <ContactInfo> { model.ContactInfoViewModel.ContactInfo }, new SqlQuery()); await Task.WhenAll(createRelationshipsTask, createContactInfoTask); return(RedirectToAction("List", Request.QueryString.ToRouteValues(new { GroupId = model.GroupId }))); }
public async Task RunAsync(IRestContext context) { // Getting database interface IDatabaseApi databaseApi = context.Factory.CreateDatabaseApi(ServiceName); // List available tables IEnumerable <TableInfo> tables = (await databaseApi.GetTableNamesAsync(true)).ToList(); Console.WriteLine("Existing tables: {0}", tables.Select(x => x.Name).ToStringList()); foreach (TableInfo table in tables) { Console.WriteLine("\t{0}", table.Name); } // Delete staff table if it exists if (tables.Any(x => x.Name == TableName)) { Console.WriteLine("Deleting table {0}...", TableName); if (await databaseApi.DeleteTableAsync(TableName)) { Console.WriteLine("Table deleted."); } } Console.WriteLine("Creating {0} table schema...", TableName); TableSchema staffTableSchema = CreateTestTableSchema(); await databaseApi.CreateTableAsync(staffTableSchema); // Describe table staffTableSchema = await databaseApi.DescribeTableAsync(TableName); Console.WriteLine("Got {0} table schema, table's label is {1}", TableName, staffTableSchema.Label); // Create new record Console.WriteLine("Creating {0} records...", TableName); List <StaffRecord> records = CreateStaffRecords().ToList(); records = (await databaseApi.CreateRecordsAsync(TableName, records, new SqlQuery())).Records; // Update record Console.WriteLine("Updating {0} records...", TableName); StaffRecord firstRecord = records.First(); firstRecord.FirstName = "Andrei 2"; await databaseApi.UpdateRecordsAsync(TableName, records, new SqlQuery()); SqlQuery query = new SqlQuery { Filter = "Age > 30", Order = "Age", Fields = "*" }; IEnumerable <StaffRecord> selection = (await databaseApi.GetRecordsAsync <StaffRecord>(TableName, query)).Records; string ages = selection.Select(x => x.Age.ToString(CultureInfo.InvariantCulture)).ToStringList(); Console.WriteLine("Get records with SqlQuery: ages={0}", ages); // Deleting one record Console.WriteLine("Deleting second record..."); await databaseApi.DeleteRecordsAsync(TableName, records.Skip(1).Take(1), new SqlQuery()); // Get table records records = (await databaseApi.GetRecordsAsync <StaffRecord>(TableName, new SqlQuery())).Records; Console.WriteLine("Retrieved {0} records:", TableName); foreach (StaffRecord item in records) { Console.WriteLine("\t{0}", item); } }