public async Task <Group> CreateOrUpdateGroupAsync(Group group) { using (var ctx = new EmailServiceDbEntities(this.connectionString)) { var entity = await ctx.Groups.SingleOrDefaultAsync( s => s.EngagementAccount == group.EngagementAccount && s.Name == group.Name); if (entity == null) { entity = new GroupEntity(); entity.Name = group.Name; entity.EngagementAccount = group.EngagementAccount; entity.Description = group.Description; entity.Properties = JsonConvert.SerializeObject(group.Properties); entity.Created = entity.Modified = DateTime.UtcNow; ctx.Groups.Add(entity); } else { entity.Description = group.Description; entity.Properties = JsonConvert.SerializeObject(group.Properties); entity.Modified = DateTime.UtcNow; } await ctx.SaveChangesAsync(); return(entity.ToModel()); } }
public static Group ToModel(this GroupEntity entity) { var group = new Group(); group.EngagementAccount = entity.EngagementAccount; group.Name = entity.Name; group.Description = entity.Description; group.Properties = JsonConvert.DeserializeObject <PropertyCollection <string> >(entity.Properties); return(group); }