public int SwitchLinkPresenceInGroup(string userId, int linkId, int groupId) { var user = UserRepository.Find(u => u.Id == userId); if (user != null) { var link = LinkRepository.GetById(linkId); if (link != null) { var group = GroupOfLinksRepository.Find(g => g.Id == groupId); if (group != null) { if (link.Groups.Select(i => i.Id).Contains(groupId)) { link.Groups.Remove(group); LinkRepository.Update(link); return(link.Id); } link.Groups.Add(group); LinkRepository.Update(link); return(link.Id); } throw new Exception("Group is not found"); } throw new Exception("Link is not found"); } throw new Exception("User is not found"); }
public LinksForGroupViewModel GetAllLinksForGroup(string userId, int groupId, bool showAll) { var model = new LinksForGroupViewModel(); model.ListOfLinks = new List <LinkViewModel>(); var user = UserRepository.Find(u => u.Id == userId && u.Status == Domain.Entities.StatusEnums.EntityStatus.Active); if (user != null) { var group = GroupOfLinksRepository.Find(g => g.Id == groupId && g.Status == Domain.Entities.StatusEnums.EntityStatus.Active); if (group != null) { model.Id = groupId; model.Name = group.Name; if (showAll) { model.ListOfLinks = LinkRepository.FindAll(w => w.SocialNetwork.Id == group.SocialNetwork.Id && w.Groups.FirstOrDefault(g => g.User.Id == userId) != null && w.Status == Domain.Entities.StatusEnums.EntityStatus.Active) .Select(s => new LinkViewModel { Id = s.Id, Name = s.Name, Url = s.Url, DateOfCreation = s.DateOfCreation, GroupId = s.Groups.Select(i => i.Id).Contains(group.Id) ? groupId : 0 }).ToList(); } else { model.ListOfLinks = LinkRepository.FindAll(w => w.Groups.FirstOrDefault(g => g.User.Id == userId && g.Id == groupId) != null && w.Status == Domain.Entities.StatusEnums.EntityStatus.Active) .Select(s => new LinkViewModel { Id = s.Id, Name = s.Name, Url = s.Url, DateOfCreation = s.DateOfCreation, GroupId = s.Groups.Select(i => i.Id).Contains(group.Id) ? groupId : 0 }).ToList(); } } else { throw new Exception("Group is not found"); } } else { throw new Exception("User is not found"); } return(model); }
public int EditGroupOfLinks(string userId, int socialNetworkId, GroupOfLinksViewModel model) { if (model.Name != null && model.Name != "") { if (model.Id != 0) { var group = GroupOfLinksRepository.Find(g => g.Id == model.Id && g.Status == Domain.Entities.StatusEnums.EntityStatus.Active); if (group != null) { group.Name = model.Name; GroupOfLinksRepository.Update(group); return(group.Id); } throw new Exception("Can`t find the group"); } else { var existedGroups = GroupOfLinksRepository.Find(g => g.Name == model.Name && g.SocialNetwork.Id == socialNetworkId && g.User.Id == userId && g.Status == Domain.Entities.StatusEnums.EntityStatus.Active); if (existedGroups != null) { throw new Exception("Group with the same name already exist"); } var sn = SocialNetworkRepository.Find(s => s.Id == socialNetworkId && s.Status == Domain.Entities.StatusEnums.EntityStatus.Active); var user = UserRepository.Find(u => u.Id == userId && u.Status == Domain.Entities.StatusEnums.EntityStatus.Active); if (sn != null && user != null) { GroupOfLinks group = new GroupOfLinks(); group.DateOfCreation = DateTime.Now; group.IsTemp = false; group.Name = model.Name; group.SocialNetwork = sn; group.User = user; group.Status = Domain.Entities.StatusEnums.EntityStatus.Active; GroupOfLinksRepository.Insert(group); return(group.Id); } throw new Exception("Can`t find social network or not enought permissions"); } } throw new Exception("Name can`t be empty"); }
public int EditLink(LinkViewModel model, string userId) { var group = GroupOfLinksRepository.Find(g => g.Id == model.GroupId); if (model.Id != 0) { var link = LinkRepository.Find(w => w.Id == model.Id && w.Groups.FirstOrDefault(u => u.User.Id == userId) != null); if (link != null) { link.Name = model.Name; link.Url = model.Url; if (!link.Groups.Contains(group)) { link.Groups.Add(group); } LinkRepository.Update(link); return(link.Id); } } else { //Different user can create same links var link = LinkRepository.Find(w => w.Url == model.Url && w.Groups.FirstOrDefault(u => u.User.Id == userId) != null && w.Status == Domain.Entities.StatusEnums.EntityStatus.Active); if (group == null) { throw new Exception("Group is not found"); } if (link != null) { if (link.Name != model.Name) { throw new Exception("Link already exist. It name is " + link.Name); } if (!link.Groups.Contains(group)) { link.Groups.Add(group); LinkRepository.Update(link); return(link.Id); } } else { Link lnk = new Link() { Name = model.Name, Url = model.Url, Status = Domain.Entities.StatusEnums.EntityStatus.Active, DateOfCreation = DateTime.UtcNow, SocialNetwork = group.SocialNetwork }; lnk.Groups = new List <GroupOfLinks>() { group }; LinkRepository.Insert(lnk); return(lnk.Id); } } return(-1); }
public GroupOfLinks GetGroupById(int id, string userId) { return(GroupOfLinksRepository.Find(g => g.Id == id && g.User.Id == userId && g.Status == Domain.Entities.StatusEnums.EntityStatus.Active)); }