public IHttpActionResult PostShoutGroup(ShoutGroupDto shoutGroupDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } ShoutGroup shoutGro = new ShoutGroup() { ID = shoutGroupDto.ID, Name = shoutGroupDto.Name, TrackCost = shoutGroupDto.TrackCost, Category = shoutGroupDto.Category, Shouts = new List <Shout>(), ShoutUsers = new List <ShoutUser>() }; var shoutUsersForGroup = new List <ShoutUser>(); foreach (var shoutGroupUser in shoutGroupDto.Users) { ShoutUser foundUser = null; if (shoutGroupUser.ID != null && shoutGroupUser.ID != Guid.Empty) { foundUser = db.ShoutUsers.FirstOrDefault(x => x.ID == shoutGroupUser.ID); } else if (foundUser == null && !String.IsNullOrEmpty(shoutGroupUser.Email)) { foundUser = db.ShoutUsers.FirstOrDefault(x => x.Email.Equals(shoutGroupUser.Email, StringComparison.OrdinalIgnoreCase)); } if (foundUser != null) { shoutGro.ShoutUsers.Add(foundUser); if (foundUser.ShoutGroups == null) { foundUser.ShoutGroups = new List <ShoutGroup>(); } foundUser.ShoutGroups.Add(shoutGro); shoutUsersForGroup.Add(foundUser); } else { var newUser = new ShoutUser() { ID = Guid.NewGuid(), Email = shoutGroupUser.Email, UserName = shoutGroupUser.UserName }; if (newUser.ShoutGroups == null) { newUser.ShoutGroups = new List <ShoutGroup>(); } newUser.ShoutGroups.Add(shoutGro); shoutGro.ShoutUsers.Add(newUser); db.ShoutUsers.Add(newUser); } } foreach (var sh in shoutUsersForGroup) { db.ShoutUsers.Attach(sh); } ShoutGroupIcon shoutGroupIcon = new ShoutGroupIcon() { ShoutGroupID = shoutGro.ID, ShoutGroup = shoutGro, ShoutIconIndex = shoutGroupDto.ShoutGroupIconIndex }; shoutGro.GroupIcon = shoutGroupIcon; db.ShoutGroups.Add(shoutGro); try { db.SaveChanges(); } catch (DbUpdateException) { if (ShoutGroupExists(shoutGroupDto.ID)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = shoutGroupDto.ID }, shoutGroupDto)); }
protected override void Seed(WhoseShoutWebService.Models.ApplicationDbContext context) { var shout = new Shout { ID = new Guid("45def82e-2b68-47e1-98c6-7d34906b46f1"), ShoutGroupID = new Guid("bf3641d1-a384-494d-a957-18f2aa42170c"), ShoutUserID = new Guid("d9c91004-3994-4bb4-a703-267904985126"), Cost = 9.0f, PurchaseTimeUtc = DateTime.UtcNow }; var user1 = new ShoutUser { ID = new Guid("d9c91004-3994-4bb4-a703-267904985126"), UserName = "******", Email = "*****@*****.**", FacebookID = "11111111111111111", ShoutGroups = new List <ShoutGroup>(), Shouts = new List <Shout>() }; var user2 = new ShoutUser { ID = new Guid("c9c9f88b-853b-46e5-a70a-fad212fab6b0"), UserName = "******", Email = "*****@*****.**", TwitterID = "12519262411111111111", ShoutGroups = new List <ShoutGroup>(), Shouts = new List <Shout>() }; var user3 = new ShoutUser { ID = new Guid("840a9916-ca86-4575-9025-6adb2abfa389"), UserName = "******", Email = "*****@*****.**", FacebookID = "11111111111111112", ShoutGroups = new List <ShoutGroup>(), Shouts = new List <Shout>() }; var shoutGroup = new ShoutGroup { ID = new Guid("bf3641d1-a384-494d-a957-18f2aa42170c"), Category = "Coffee", Name = "CoffeeTime", ShoutUsers = new List <ShoutUser>(), Shouts = new List <Shout>() }; var shoutGroup2 = new ShoutGroup { ID = new Guid("9befd37c-62c6-4fcf-9d77-8945c7964e7b"), Category = "Beer", Name = "Beeeeers", ShoutUsers = new List <ShoutUser>(), Shouts = new List <Shout>() }; ShoutGroupIcon icon = new ShoutGroupIcon() { ShoutGroupID = shoutGroup2.ID, ShoutIconIndex = 0, ShoutGroup = shoutGroup2 }; shout.ShoutUser = user1; shout.ShoutGroup = shoutGroup; user1.ShoutGroups.Add(shoutGroup); user2.ShoutGroups.Add(shoutGroup); user1.Shouts.Add(shout); shoutGroup.ShoutUsers.Add(user1); shoutGroup.ShoutUsers.Add(user2); shoutGroup.Shouts.Add(shout); shoutGroup2.ShoutUsers.Add(user3); context.Shouts.AddOrUpdate(p => p.ID, shout); context.ShoutUsers.AddOrUpdate(p => p.ID, user1, user2, user3); context.ShoutGroups.AddOrUpdate(p => p.ID, shoutGroup, shoutGroup2); }
public IHttpActionResult PutShoutGroup(Guid id, ShoutGroupDto shoutGroupDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != shoutGroupDto.ID) { return(BadRequest()); } ShoutGroup shoutGroup = db.ShoutGroups.Include(sgi => sgi.GroupIcon).FirstOrDefault(x => x.ID == id); db.ShoutGroups.Attach(shoutGroup); shoutGroup.Name = shoutGroupDto.Name; shoutGroup.TrackCost = shoutGroupDto.TrackCost; shoutGroup.Category = shoutGroupDto.Category; if (shoutGroup.GroupIcon == null) { ShoutGroupIcon sgi = new ShoutGroupIcon() { ShoutGroup = shoutGroup, ShoutIconIndex = shoutGroupDto.ShoutGroupIconIndex, }; shoutGroup.GroupIcon = sgi; } else { shoutGroup.GroupIcon.ShoutIconIndex = shoutGroupDto.ShoutGroupIconIndex; } //var shoutUsersForGroup = new List<ShoutUser>(); //foreach (var shoutGroupUser in shoutGroupDto.Users) //{ // ShoutUser foundUser = null; // if (shoutGroupUser.ID != null && shoutGroupUser.ID != Guid.Empty) // { // foundUser = db.ShoutUsers.FirstOrDefault(x => x.ID == shoutGroupUser.ID); // } // else if (foundUser == null && !String.IsNullOrEmpty(shoutGroupUser.Email)) // { // foundUser = db.ShoutUsers.FirstOrDefault(x => x.Email.Equals(shoutGroupUser.Email, StringComparison.OrdinalIgnoreCase)); // } // if (foundUser != null) // { // shoutGroup.ShoutUsers.Add(foundUser); // if (foundUser.ShoutGroups == null) // { // foundUser.ShoutGroups = new List<ShoutGroup>(); // } // //foundUser.ShoutGroups.Add(shoutGroup); // //foundUser.ShoutGroups.FirstOrDefault(x => x.ID ==) // shoutUsersForGroup.Add(foundUser); // } // else // { // var newUser = new ShoutUser() { ID = Guid.NewGuid(), Email = shoutGroupUser.Email, UserName = shoutGroupUser.UserName }; // if (newUser.ShoutGroups == null) // { // newUser.ShoutGroups = new List<ShoutGroup>(); // } // newUser.ShoutGroups.Add(shoutGroup); // shoutGroup.ShoutUsers.Add(newUser); // db.ShoutUsers.Add(newUser); // } //} //foreach (var sh in shoutUsersForGroup) //{ // db.ShoutUsers.Attach(sh); //} db.Entry(shoutGroup).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ShoutGroupExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }