/// <summary> /// Remove user from the specified group /// </summary> /// <param name="userId">Id of the user, who wants to leave the group</param> /// <param name="groupId">Id of the group that the user wants to leave</param> /// <returns>none</returns> public static void LeaveGroup(string userId, int groupId) { using (var ctx = new PiChatDbContext()) { // REVIEW: SingleOrDefault().Role jól elszáll, ha nem talál elemet. var groupMembership = ctx.GroupMemberships.Where(c => (c.UserId == userId && c.GroupId == groupId)).SingleOrDefault(); // REVIEW: itt látszik jól, hogy a kontroll a returnnél átesik a hívóhoz, ezért jobb lenne a következő sorba törni. if (groupMembership == null) { throw new PiChatEntityNotFoundException(); } if (groupMembership.Role == GroupMembershipRole.Owner || groupMembership.Role < GroupMembershipRole.Member) { throw new PiChatGenericException(String.Format("You cant unsubscribe from a group while your role is '{0}'", groupMembership.Role)); } ctx.GroupMemberships.Remove(groupMembership); ctx.SaveChanges(); ////régi //if (ctx.GroupMemberships.Where(c => (c.UserId.Equals(userId) && c.GroupId == groupId)).SingleOrDefault() != null) return; //var groupMembership = new GroupMembership { UserId = userId, GroupId = groupId }; //ctx.GroupMemberships.Attach(groupMembership); //ctx.GroupMemberships.Remove(groupMembership); //ctx.SaveChanges(); } }
/// <summary> /// Create a new group /// </summary> /// <param name="userId">Id of the user, who wants to create the group</param> /// <param name="groupName">The name of the new group which is created</param> /// <param name="isPrivate">Specifies if the group should be private or not</param> /// <returns>A groupModel of the new group</returns> public static GroupModel CreateGroup(string userId, string groupName, bool isPrivate) { using (var ctx = new PiChatDbContext()) { var group = new Group { Name = groupName, OwnerId = userId, Description = "", IsPrivate = isPrivate }; ctx.Groups.Add(group); var groupMembership = new GroupMembership { UserId = userId, Group = group, Role = GroupMembershipRole.Owner }; ctx.GroupMemberships.Add(groupMembership); ctx.SaveChanges(); return(ctx.Groups.Include(g => g.Owner) .Include(g => g.Members) .Single(g => g.Id == group.Id) .MapTo(g => new GroupModel { OwnerName = g.Owner.Name, Role = GroupMembershipRole.Owner, MembersCount = 1, PicturesCount = 0 })); } }
/// <summary> /// Remove the specified image /// </summary> /// <param name="imageId">Specifies the image</param> /// <param name="userId">Specifies the user who executes the query</param> /// <returns></returns> public static ImageModel RemoveImage(int imageId, string userId) { using (var ctx = new PiChatDbContext()) { //TODO Admin is és csoport tulaj is tudjon törölni var image = ctx.Images.Find(imageId); if (image == null) { throw new PiChatEntityNotFoundException(); } if (ctx.GroupMemberships.Find(image.GroupId, userId).Role >= GroupMembershipRole.Administrator || image.OwnerId == userId) { ctx.Images.Remove(image); ctx.SaveChanges(); } else { throw new PiChatNotAuthorizedException(); } return(image.MapTo(i => new ImageModel { ImageId = i.Id })); } }
/// <summary> /// Changes a membership role from pending to member /// </summary> /// <param name="membershipId">Specifies the entry in the membership table</param> /// <param name="userId">Specifies the user who executes the query</param> /// <returns> </returns> public static void AcceptMembershipRequests(int membershipId, string userId) { using (var ctx = new PiChatDbContext()) { var membership = ctx.GroupMemberships .Where(gm => gm.GroupMembershipId == membershipId && gm.Role == GroupMembershipRole.Pending).FirstOrDefault(); if (membership == null) { throw new PiChatEntityNotFoundException(); } var admin = ctx.GroupMemberships.Find(membership.GroupId, userId); if (admin == null) { throw new PiChatEntityNotFoundException(); } if (admin.Role < GroupMembershipRole.Administrator) { throw new PiChatNotAuthorizedException(); } membership.Role = GroupMembershipRole.Member; ctx.SaveChanges(); } }
/// <summary> /// Deletes an entry from the membership table given by a membershipId where the role is pending /// </summary> /// <param name="membershipId">Specifies the entry in the membership table</param> /// <param name="userId">Specifies the user who executes the query</param> /// <returns> </returns> public static void RejectMembershipRequests(int membershipId, string userId) { using (var ctx = new PiChatDbContext()) { // REVIEW: Select(x => x) ? //solved --vazul eltávolítottam a fölös selectet var membership = ctx.GroupMemberships .Where(gm => gm.GroupMembershipId == membershipId && gm.Role == GroupMembershipRole.Pending).FirstOrDefault(); if (membership == null) { throw new PiChatEntityNotFoundException(); } var admin = ctx.GroupMemberships.Find(membership.GroupId, userId); if (admin == null) { throw new PiChatEntityNotFoundException(); } if (admin.Role < GroupMembershipRole.Administrator) { throw new PiChatNotAuthorizedException(); } ctx.GroupMemberships.Remove(membership); ctx.SaveChanges(); } }
/// <summary> /// Changes the name of the user /// </summary> /// <param name="userId">Specifies which users name we want to change</param> /// <param name="newName">Specifies the new name we want to change the old name to</param> public void ChangeName(string userId, string newName) { using (var ctx = new PiChatDbContext()) { var user = ctx.Users.Where(u => u.Id == userId).Select(u => u).First(); user.Name = newName; ctx.SaveChanges(); } }
/// <summary> /// Add user to the specified group /// </summary> /// <param name="userId">Id of the user, who wants to enter the group</param> /// <param name="groupId">Id for the group that the user wants to enter to</param> /// <returns>none</returns> public static void JoinToGroup(string userId, int groupId) { using (var ctx = new PiChatDbContext()) { GroupMembership membership; if ((membership = ctx.GroupMemberships.Where(c => (c.UserId == userId && c.GroupId == groupId)).SingleOrDefault()) != null) { throw new PiChatGenericException(String.Format("You have {0} role in the group already", membership.Role.ToString())); } #region Review // REVIEW: az üres try-catch mindig gyanús. Also, ismét, SingleOrDefault helyett nekünk összesen egy bool érték kell, erre Any() és All() valók. //bool isPrivate; //try //{ // isPrivate = ctx.Groups.Where(g => g.Id == groupId).Select(g => g.IsPrivate).SingleOrDefault(); //} //catch //{ // return; //} // Helyette: //var isPrivate = ctx.Groups.Any(g => g.Id == groupId && g.IsPrivate); // REVIEW: A group vagy user nem létezését nem kezeljük. // Mivel nem csinálunk vele semmi többet, ezért a groupMembership változóra nincs szükség. #endregion // Review solve: --vazul if (!ctx.Users.Any(u => u.Id == userId)) { throw new PiChatEntityNotFoundException( ); } var group = ctx.Groups.Find(groupId); if (group == null) { throw new PiChatEntityNotFoundException(); } var groupMembership = new GroupMembership { UserId = userId, GroupId = groupId, Role = group.IsPrivate ? GroupMembershipRole.Pending : GroupMembershipRole.Member }; ctx.GroupMemberships.Add(groupMembership); ctx.SaveChanges(); } }
/// <summary> /// Renames a group /// </summary> /// <param name="userId">Specifies the user who wants to rename the group</param> /// <param name="groupId">Specifies which group the user wants to rename</param> /// <param name="newName">Specifies which name do the user want to rename the group to</param> public static void RenameGroup(string userId, int groupId, string newName) { using (var ctx = new PiChatDbContext()) { var membership = ctx.GroupMemberships.Include(gm => gm.Group).Where(gm => gm.GroupId == groupId && gm.UserId == userId).SingleOrDefault(); if (membership == null) { throw new PiChatEntityNotFoundException(); } if (membership.Role < GroupMembershipRole.Administrator) { throw new PiChatNotAuthorizedException(); } membership.Group.Name = newName; ctx.SaveChanges(); var group = ctx.Groups.Single(g => g.Id == groupId); if (group.OwnerId == userId) { group.Name = newName; ctx.SaveChanges(); } } }
/// <summary> /// Delete the group with the given id, WARNING : WILL CASCADE DELETE /// </summary> /// <param name="groupId">Specifies which group we want to delete</param> /// <param name="userId">Specifies the user who wants to execute the delete</param> public static void DeleteGroup(int groupId, string userId) { using (var ctx = new PiChatDbContext()) { var group = ctx.Groups.Where(g => g.Id == groupId).SingleOrDefault(); if (group == null) { throw new PiChatEntityNotFoundException(); } if (group.OwnerId != userId) { throw new PiChatNotAuthorizedException(); } ctx.Groups.Remove(group); ctx.SaveChanges(); } }
/// <summary> /// Changes the description of a group /// </summary> /// <param name="userId">Specifies the user who wants to change the description of the group</param> /// <param name="groupId">Specifies which group's description do the user wants to change</param> /// <param name="newDesc">Specifies which description do the user want to change the group's description to</param> public static void ChangeDescription(string userId, int groupId, string newDesc) { using (var ctx = new PiChatDbContext()) { var membership = ctx.GroupMemberships.Where(m => m.UserId == userId && m.GroupId == groupId).Include(m => m.Group).SingleOrDefault(); if (membership == null) { throw new PiChatEntityNotFoundException(); } if (membership.Role < GroupMembershipRole.Administrator) { throw new PiChatNotAuthorizedException(); } membership.Group.Description = newDesc; ctx.SaveChanges(); } }
/// <summary> /// Add a new image to the specified group, returns the new image's id /// </summary> /// <param name="userId">The user who executes the query</param> /// <param name="groupId">Specifies the group</param> /// <param name="imagePath">Specifies the imagePath</param> /// <param name="description">Specifies a description for the image</param> /// <param name="uploadTime">specifies the time when the upload happened</param> /// <returns>The id of the new image</returns> public static int AddImage(string userId, int groupId, string imagePath, DateTime uploadTime, string description = "") { using (var ctx = new PiChatDbContext()) { var membership = ctx.GroupMemberships.Find(groupId, userId); if (membership.Role < GroupMembershipRole.Member) { throw new PiChatNotAuthorizedException(); } var image = new Image { GroupId = groupId, OwnerId = userId, Path = imagePath, UploadTime = uploadTime, Description = description }; ctx.Images.Add(image); ctx.SaveChanges(); return(image.Id); } }
/// <summary> /// Change description of the specified image /// </summary> /// <param name="imageId">Specifies the image</param> /// <param name="userId">Specifies the user who executes the query</param> /// <param name="NewDescription">Specifies the description which we want to change the current description</param> /// <returns></returns> public static ImageModel ChangeDescription(string userId, int imageId, string newDescription) { using (var ctx = new PiChatDbContext()) { var image = ctx.Images.Find(imageId); if (image == null) { throw new PiChatEntityNotFoundException(); } if (ctx.GroupMemberships.Find(image.GroupId, userId).Role >= GroupMembershipRole.Administrator || image.OwnerId == userId) { image.Description = newDescription; ctx.SaveChanges(); } return(image.MapTo(i => new ImageModel { ImageId = i.Id })); } }
/// <summary> /// Changes a membership role from pending to member /// </summary> /// <param name="membersEmail">Specifies the member by his email address</param> /// <param name="groupId">Specifies which group's member do we want to promote to be one of the group's administrators</param> /// <param name="userId">Specifies the user who executes the query</param> public static void GrantAdminPrivilage(string membersEmail, int groupId, string userId) { using (var ctx = new PiChatDbContext()) { var ownerMembership = ctx.GroupMemberships.Find(groupId, userId); //TODO return custom exception not normal return if (ownerMembership == null) { throw new PiChatEntityNotFoundException(); } if (ownerMembership.Role != GroupMembershipRole.Owner) { throw new PiChatNotAuthorizedException(); } var membership = ctx.GroupMemberships.Include(m => m.User).Where(m => m.User.Email == membersEmail).Where(m => m.GroupId == groupId).SingleOrDefault(); if (membership == null) { throw new PiChatEntityNotFoundException(); } membership.Role = GroupMembershipRole.Administrator; ctx.SaveChanges(); } }