public IHttpActionResult PostShoutUser(ShoutUser shoutUser) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.ShoutUsers.Add(shoutUser); try { db.SaveChanges(); } catch (DbUpdateException) { if (ShoutUserExists(shoutUser.ID)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = shoutUser.ID }, shoutUser)); }
public IHttpActionResult PatchShoutUser(Guid id, ShoutUser shoutUser) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != shoutUser.ID) { return(BadRequest()); } db.Entry(shoutUser).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ShoutUserExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult GetShoutUserBySocial(string socialId, AuthType authType) { ShoutUser shoutUser = null; if (!String.IsNullOrEmpty(socialId)) { switch (authType) { case AuthType.Facebook: shoutUser = db.ShoutUsers.FirstOrDefault(x => x.FacebookID == socialId); break; } } if (shoutUser == null) { return(NotFound()); } ShoutUserDto ret = new ShoutUserDto() { ID = shoutUser.ID, Email = shoutUser.Email, UserName = shoutUser.UserName, AuthType = AuthType.Facebook, ShoutSocialID = shoutUser.FacebookID, AvatarUrl = shoutUser.AvatarUrl }; return(Ok(ret)); }
private ShoutUser ShoutUserFromSettings(bool newID) { ShoutUser u = new ShoutUser() { UserName = Settings.Current.UserFirstName, Email = Settings.Current.UserEmail, AvatarUrl = Settings.Current.AvatarUrl }; if (newID) { u.ID = Guid.NewGuid(); } else { u.ID = Settings.Current.UserGuid; } if (Settings.Current.UserAuth == Models.AuthType.Facebook) { u.FacebookID = Settings.Current.SocialUserID; } else if (Settings.Current.UserAuth == AuthType.Twitter) { u.TwitterID = Settings.Current.SocialUserID; } return(u); }
public IHttpActionResult GetShoutUser(Guid id) { ShoutUser shoutUser = db.ShoutUsers.Find(id); if (shoutUser == null) { return(NotFound()); } return(Ok(shoutUser)); }
public IHttpActionResult DeleteShoutUser(Guid id) { ShoutUser shoutUser = db.ShoutUsers.Find(id); if (shoutUser == null) { return(NotFound()); } db.ShoutUsers.Remove(shoutUser); db.SaveChanges(); return(Ok(shoutUser)); }
public async Task GetOrCreate(bool checkSocial = true) { bool patch = false; ShoutUserDto userDto = null; if (checkSocial && !String.IsNullOrEmpty(Settings.Current.SocialUserID)) { userDto = await CurrentApp.Current.MainViewModel.ServiceApi.GetShoutUserBySocial(Settings.Current.SocialUserID); } if (userDto == null) { if (!String.IsNullOrEmpty(Settings.Current.UserEmail)) { userDto = await CurrentApp.Current.MainViewModel.ServiceApi.GetShoutUserByEmail(Settings.Current.UserEmail); } if (userDto == null) { // New user ShoutUser u = ShoutUserFromSettings(true); bool success = await CurrentApp.Current.MainViewModel.ServiceApi.NewShoutUser(u); if (success) { Settings.Current.UserGuid = u.ID; } } else { // Likely created by someone else patch = true; } } else { // Something to be updated patch = true; } if (patch) { ShoutUser u = ShoutUserFromSettings(false); //if user email is found, update that await CurrentApp.Current.MainViewModel.ServiceApi.PatchShoutUser(u); } }
public IHttpActionResult GetShoutUserByEmail(string email) { ShoutUser shoutUser = null; if (!String.IsNullOrEmpty(email)) { shoutUser = db.ShoutUsers.FirstOrDefault(x => x.Email.Equals(email, StringComparison.OrdinalIgnoreCase)); } if (shoutUser == null) { return(NotFound()); } return(Ok(shoutUser)); }
public async Task <bool> NewShoutUser(ShoutUser user) { try { using (var client = NewHttpClient()) { var response = await PostAsJsonAsync(client, "api/shoutusers", user); response.EnsureSuccessStatusCode(); } } catch (Exception) { return(false); } return(true); }
public async Task <ShoutUserDto> PatchShoutUser(ShoutUser shoutUser) { try { using (var client = NewHttpClient()) { var response = await PatchAsJsonAsync(client, "api/shoutusers/" + shoutUser.ID, shoutUser); response.EnsureSuccessStatusCode(); return(await ReadAsAsync <ShoutUserDto>(response.Content)); } } catch (Exception) { return(null); } }
public ProtectedShoutUser(ShoutUser user) { UserName = user.UserName; Id = user.Id; }
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 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)); }