// AddTagTo <albumName> <tag> public override string Execute(string[] data, PhotoShareContext context) { string albumName = data[0]; string tagText = data[1].ValidateOrTransform(); if (!context.Tags.Any(t => t.Name.Equals(tagText, StringComparison.InvariantCultureIgnoreCase)) || !context.Albums.Any(a => a.Name.Equals(albumName))) { throw new ArgumentException("Either tag or album does not exist!"); } Tag currentTag = context.Tags.SingleOrDefault(t => t.Name.Equals(tagText)); Album currentAlbum = context.Albums.SingleOrDefault(a => a.Name.Equals(albumName)); AlbumTag currentAlbumTag = new AlbumTag { Album = currentAlbum, Tag = currentTag }; currentAlbum.AlbumTags.Add(currentAlbumTag); try { context.SaveChanges(); } catch (DbUpdateException) { throw new ArgumentException($"Tag {tagText} has already been added to \"{albumName}\"!"); } return($"Tag {currentTag.Name} added to {currentAlbum.Name}!"); }
private void SeedAlbumTags() { Console.WriteLine("Add tags to albums..."); using (var db = new SocialNetworkDbContext()) { var tags = db.Tags.ToList(); var albums = db.Albums.ToList(); for (int i = 0; i < tags.Count; i++) { var tag = tags[i]; var albumsCount = random.Next(3, 10); for (int j = 0; j < albumsCount; j++) { var album = albums[random.Next(0, albums.Count)]; if (tag.Albums.All(a => a.AlbumId != album.Id)) { var albumInTag = new AlbumTag { AlbumId = album.Id }; tag.Albums.Add(albumInTag); db.SaveChanges(); } } } } }
public string Execute(string[] data) { if (data.Length < 2) { throw new ArgumentException("Invalid command!"); } var albumName = data[0]; var tagName = data[1]; var album = this.context.Albums.FirstOrDefault(a => a.Name == albumName); var tag = this.context.Tags.FirstOrDefault(a => a.Name == tagName.ValidateOrTransform()); if (tag == null || album == null) { throw new ArgumentException("Either tag or album do not exist!"); } var albumTag = new AlbumTag { TagId = tag.Id, AlbumId = album.Id }; album.AlbumTags.Add(albumTag); this.context.SaveChanges(); return($"Tag {tagName} added to {albumName} successfully!"); }
public string AddTagTo(string albumName, string tagName) { User user = Session.User; CheckCredentials(albumName, user); var album = db.Albums .Include(a => a.AlbumTags) .ThenInclude(t => t.Tag) .Include(a => a.AlbumRoles) .FirstOrDefault(a => a.Name == albumName); var tag = db.Tags .Include(t => t.AlbumTags) .ThenInclude(a => a.Album) .FirstOrDefault(t => t.Name == tagName); CheckAlbumAndTag(albumName, tagName, album, tag); var albumTag = new AlbumTag() { Album = album, Tag = tag }; db.AlbumTags.Add(albumTag); album.AlbumTags.Add(albumTag); tag.AlbumTags.Add(albumTag); db.SaveChanges(); return($"Tag {tagName} added to {albumName}!"); }
public Album Create(int userId, string albumTitle, string bgColor, string[] tags) { var album = new Album { Name = albumTitle, BackgroundColor = Enum.Parse <Color>(bgColor, true) }; this.context.Albums.Add(album); this.context.SaveChanges(); var albumRole = new AlbumRole { Album = album, UserId = userId }; this.context.AlbumRoles.Add(albumRole); this.context.SaveChanges(); foreach (var tag in tags) { var currentTagId = this.context.Tags.FirstOrDefault(x => x.Name == tag).Id; var albumTag = new AlbumTag { Album = album, TagId = currentTagId }; this.context.AlbumTags.Add(albumTag); } this.context.SaveChanges(); return(album); }
// AddTagTo <albumName> <tag> public static string Execute(string[] data) { if (Session.User == null) { throw new InvalidOperationException("Invalid credentials!"); } var albumName = data[1]; var tagName = "#" + data[2]; using (var db = new PhotoShareContext()) { var tag = db.Tags.FirstOrDefault(x => x.Name == tagName); var album = db.Albums.FirstOrDefault(x => x.Name == albumName); if (tag == null || album == null) { throw new ArgumentException("Either tag or album do not exist!"); } var albumTags = new AlbumTag() { Album = album, Tag = tag }; album.AlbumTags.Add(albumTags); db.SaveChanges(); } return($"Tag {tagName} added to {albumName}!"); }
public Album Create(int userId, string albumName, string backgroundColor, string[] tagNames) { Album album = new Album() { Name = albumName, BackgroundColor = Enum.Parse <Color>(backgroundColor, true), }; context.Albums.Add(album); context.SaveChanges(); AlbumRole albumRole = new AlbumRole() { AlbumId = album.Id, UserId = userId, Role = Role.Owner }; context.AlbumRoles.Add(albumRole); context.SaveChanges(); foreach (var tagName in tagNames) { Tag tag = context.Tags.FirstOrDefault(t => t.Name == tagName); AlbumTag albumTag = new AlbumTag() { AlbumId = album.Id, TagId = tag.Id }; context.AlbumTags.Add(albumTag); } context.SaveChanges(); return(album); }
// AddTagTo <albumName> <tag> public static string Execute(string[] data) { using (PhotoShareContext db = new PhotoShareContext()) { var albumName = data[1]; var tagName = data[2]; if (!db.Albums.Any(a => a.Name == albumName) || !db.Tags.Any(t => t.Name == tagName)) { throw new ArgumentException($"Either tag or album do not exist!"); } Album album = db.Albums .Include(a => a.AlbumTags) .Include(a => a.AlbumRoles) .ThenInclude(ar => ar.User) .FirstOrDefault(a => a.Name == albumName); Tag tag = db.Tags .FirstOrDefault(t => t.Name == tagName); AlbumTag albumTag = new AlbumTag() { Album = album, Tag = tag }; db.AlbumTags.Add(albumTag); db.SaveChanges(); return($"Tag {tag} added to {album}!"); } throw new NotImplementedException(); }
// AddTagTo <albumName> <tag> public string Execute(string[] data) { if (Session.User == null) { throw new InvalidOperationException("Invalid credentials!"); } using (PhotoShareContext context = new PhotoShareContext()) { string albumName = data[1]; string tagName = data[2]; if (context.Albums.Any(a => a.Name != albumName) || context.Tags.Any(t => t.Name != tagName)) { throw new ArgumentException("Either tag or album do not exist!"); } var album = context.Albums.FirstOrDefault(a => a.Name == albumName); var tag = context.Tags.FirstOrDefault(t => t.Name == tagName); var albumTag = new AlbumTag() { Album = album, Tag = tag }; album.AlbumTags.Add(albumTag); context.SaveChanges(); return($"Tag{tag} added to {album}!"); } }
// AddTagTo <albumName> <tag> public static string Execute(string[] data) { var albumName = data[1]; var tagName = data[2]; if (data.Length < 3) { throw new IndexOutOfRangeException($"Command {data[0]} not valid!"); } if (Session.User == null) { throw new ArgumentException("Invalid credentials!"); } using (var db = new PhotoShareContext()) { var album = db.Albums.FirstOrDefault(e => e.Name == albumName); var tag = db.Tags.FirstOrDefault(e => e.Name == "#" + tagName); if (album == null || tag == null) { throw new ArgumentException("Either tag or album do not exist!"); } var albumTag = new AlbumTag() { Album = album, Tag = tag }; db.AlbumTags.Add(albumTag); db.SaveChanges(); return($"Tag {tagName} added to {albumName}!"); } }
public string AddTagTo(string albumName, string tagName, int userId) { var album = context.Albums.Include(a => a.AlbumRoles).SingleOrDefault(a => a.Name == albumName); var tag = context.Tags.SingleOrDefault(t => t.Name == "#" + tagName); if (album == null || tag == null) { throw new ArgumentException("Either tag or album do not exist!"); } var role = album.AlbumRoles.SingleOrDefault(ar => ar.UserId == userId); if (role == null || !role.Role.Equals(Role.Owner)) { throw new InvalidOperationException($"Invalid credentials!"); } var albumTag = new AlbumTag { Album = album, Tag = tag }; context.AlbumTags.Add(albumTag); context.SaveChanges(); return($"Tag {tagName} added to {albumName}!"); }
public Album Create(int userId, string albumTitle, string BgColor, params string[] tags) { var backgroundColors = Enum.Parse <Color>(BgColor, true); Tag[] tagEntities = context.Tags.Where(t => tags.Contains(t.Name)).ToArray(); Album album = new Album { Name = albumTitle, BackgroundColor = backgroundColors }; this.context.Albums.Add(album); for (int i = 0; i < tagEntities.Count(); i++) { AlbumTag albumTag = new AlbumTag { AlbumId = album.Id, TagId = tagEntities[i].Id }; } this.context.SaveChanges(); return(album); }
// AddTagTo <albumName> <tag> public static string Execute(string[] data) { var albumName = data[0]; var tagName = data[1].ValidateOrTransform(); using (var context = new PhotoShareContext()) { var tag = context.Tags.FirstOrDefault(t => t.Name == tagName); var album = context.Albums.FirstOrDefault(a => a.Name == albumName); if (!(tag != null && album != null)) { throw new ArgumentException($"Either tag or album do not exist!"); } var albumTag = new AlbumTag() { Album = album, Tag = tag }; context.AlbumTags.Add(albumTag); context.SaveChanges(); } return($"Tag {tagName} added to {albumName}!"); }
// AddTagTo <albumName> <tag> public override string Execute(string[] data) { if (data.Length < DataLength) { throw new ArgumentException(string.Format(ErrorMessages.InvalidCommandName, nameof(AddFriendCommand))); } var albumName = data[1]; var tagName = data[2]; using (var context = new PhotoShareContext()) { if (!context.Albums.Any(a => a.Name == albumName) || !context.Tags.Any(t => t.Name == "#" + tagName)) { throw new ArgumentException(ErrorMessages.NonExistingTagOrAlbum); } var album = context.Albums.Single(a => a.Name.Equals(albumName)); var tag = context.Tags.Single(t => t.Name.Equals(TagPrefix + tagName)); var albumTag = new AlbumTag { Album = album, Tag = tag }; context.AlbumTags.Add(albumTag); context.SaveChanges(); } return(string.Format(Messages.AddedTagToAlbum, tagName, albumName)); }
// AddTagTo <albumName> <tag> public string Execute(params string[] args) { string albumTitle = args[0]; string tagName = args[1]; if (Session.User == null || !Session.User.AlbumRoles.Any(ar => ar.Album.Name == albumTitle)) { throw new InvalidOperationException("Invalid credentials!"); } if (!Checker.AlbumExists(albumTitle) || !Checker.TagsExists(tagName)) { throw new ArgumentException("Either tag or album do not exist!"); } using (var db = new PhotoShareContext()) { Album album = db.Albums.FirstOrDefault(a => a.Name == albumTitle); Tag tag = db.Tags.FirstOrDefault(t => t.Name == tagName); AlbumTag albumTag = new AlbumTag() { Album = album, Tag = tag, }; album.AlbumTags.Add(albumTag); db.SaveChanges(); } return($"Tag {tagName} added to {albumTitle}!"); }
// AddTagTo <albumName> <tag> public static string Execute(string[] data) { var albumName = data[1]; var albumTag = data[2]; using (var db = new PhotoShareContext()) { var album = db.Albums .Where(x => x.Name == albumName) .SingleOrDefault(); var tag = db.Tags .Where(x => x.Name == albumTag) .SingleOrDefault(); if (album == null || tag == null) { throw new ArgumentException($"Either tag or album do not exist!"); } var newAlbum = new AlbumTag() { Album = album, Tag = tag }; db.AlbumTags.Add(newAlbum); db.SaveChanges(); return($"Tag #{tag} added to {album}!"); } }
public Album Create(int userId, string albumTitle, string BgColor, string[] tags) { var bgColorAsEnum = Enum.Parse <Color>(BgColor); var album = new Album { Name = albumTitle, BackgroundColor = bgColorAsEnum }; this.context.Albums.Add(album); foreach (var tag in tags) { var tagId = this.context.Tags .FirstOrDefault(t => t.Name.Equals(tag)).Id; var albumTag = new AlbumTag { Album = album, TagId = tagId }; this.context.AlbumTags.Add(albumTag); } this.context.SaveChanges(); return(album); }
// AddTagTo <albumName> <tag> public static string Execute(string[] data) { string albumName = data[0]; string tagName = TagUtilities.ValidateOrTransform(data[1]); using (PhotoShareContext context = new PhotoShareContext()) { Tag tag = context.Tags.SingleOrDefault(t => t.Name == tagName); Album album = context.Albums.SingleOrDefault(a => a.Name == albumName); if (tag == null || album == null) { throw new ArgumentException("Either album or tag do not exists!"); } AlbumTag albumTag = new AlbumTag() { AlbumId = album.Id, Album = album, TagId = tag.Id, Tag = tag }; if (context.AlbumTags.Any(at => at == albumTag)) { throw new InvalidOperationException($"Tag {tag.Name} is already added to album {album.Name} !"); } album.AlbumTags.Add(albumTag); context.SaveChanges(); } return($"Tag {tagName} added to {albumName}"); }
// AddTagTo <albumName> <tag> public static string Execute(string[] data, Session session) { if (!session.IsLoggedIn()) { throw new InvalidOperationException("Invalid credentials!"); } string albumName = data[0]; string tagName = data[1].ValidateOrTransform(); using (PhotoShareContext context = new PhotoShareContext()) { Album album = context.Albums .Include(a => a.AlbumTags) .Include(a => a.AlbumRoles) .ThenInclude(ar => ar.User) .FirstOrDefault(a => a.Name == albumName); if (album == null) { throw new ArgumentException($"Album {albumName} do not exist!"); } bool isUserOwner = album.AlbumRoles .Any(ar => ar.Role == Role.Owner && ar.User.Username == session.User.Username); if (!isUserOwner) { throw new InvalidOperationException("Invalid credentials!"); } Tag tag = context.Tags .FirstOrDefault(t => t.Name == tagName); if (tag == null) { throw new ArgumentException($"Tag {tagName} do not exist!"); } AlbumTag albumTag = new AlbumTag() { Album = album, Tag = tag }; if (album.AlbumTags.Any(at => at.AlbumId == album.Id && at.TagId == tag.Id)) { throw new InvalidOperationException($"Tag {tag.Name} is already added to album {album.Name}"); } album.AlbumTags.Add(albumTag); context.SaveChanges(); } return($"Tag {tagName} added to {albumName}!"); }
public AlbumTag AddTagTo(int albumId, int tagId) { AlbumTag albumTag = new AlbumTag() { AlbumId = albumId, TagId = tagId }; this.context.AlbumTags.Add(albumTag); this.context.SaveChanges(); return(albumTag); }
public void AddTagToAlbum(Album album, Tag tag) { AlbumTag albumTag = new AlbumTag { Album = album, Tag = tag }; context.AlbumTags.Add(albumTag); context.SaveChanges(); }
// AddTagTo <albumName> <tag> public static string Execute(string[] data) { string albumName = data[1]; string tagName = data[2]; var loggedUser = Session.User; if (loggedUser == null) { throw new InvalidOperationException("Invalid credentials! You have to login first"); } using (var db = new PhotoShareContext()) { var album = db.Albums .Include(a => a.AlbumRoles) .ThenInclude(u => u.User) .Where(a => a.Name == albumName) .FirstOrDefault(); if (album == null) { throw new ArgumentException("Either tag or album do not exist!"); } var tag = db.Tags .Where(t => t.Name == "#" + tagName) .FirstOrDefault(); if (tag == null) { throw new ArgumentException("Either tag or album do not exist!"); } //if loggedUser try to add tag to album he doesnt own bool isLoggedUserOwnTheAlbum = album.AlbumRoles .Any(u => u.User.Username == loggedUser.Username && u.Role == Role.Owner); if (!isLoggedUserOwnTheAlbum) { throw new InvalidOperationException($"Invalid credentials! You cannot add tag to album you don't own."); } var albumTag = new AlbumTag { Album = album, Tag = tag }; db.AlbumTags.Add(albumTag); db.SaveChanges(); string result = $"Tag {tagName} added to {albumName}!"; return(result); } }
private static void SeedTags(SocialNetworkDbContext db) { int totalTags = db.Albums.Count() * 20; var validtags = new List <Tag>(); for (int i = 0; i < totalTags; i++) { var tag = new Tag { Name = TagTransformer.Transform($"tag{i}") }; validtags.Add(tag); } db.Tags.AddRange(validtags); db.SaveChanges(); Console.WriteLine("Tags created successfully!"); var albumIds = db.Albums.Select(a => a.Id).ToList(); foreach (var tag in validtags) { var totalAlbum = random.Next(0, 20); for (int i = 0; i < totalAlbum; i++) { var albumId = albumIds[random.Next(0, albumIds.Count)]; var tagExistForAlbums = db.Albums .Any(a => a.Id == albumId && a.Tags.Any(t => t.TagId == tag.Id)); if (tagExistForAlbums) { i--; continue; } var albumTag = new AlbumTag() { TagId = tag.Id, AlbumId = albumId }; tag.Albums.Add(albumTag); db.SaveChanges(); } Console.WriteLine("AlbumTags created successfully!"); } }
public static AlbumTag Get(AlbumTag item) { try { using (var db = new Entities(ConnectionStringHelper.ConnectionString())) { return(db.AlbumTag.Where(r => r.AlbumId == item.AlbumId && r.TagId == item.TagId).FirstOrDefault()); } } catch (EntityException e) { throw e; } }
// AddTagTo <album> <tag> public string Execute(string[] arguments) { Check.CheckLength(2, arguments); Authentication.Authorize();; string albumName = arguments[0]; string tagName = arguments[1]; User currentUser = Authentication.GetCurrentUser(); PhotoShareContext dbContext = new PhotoShareContext(); Album album = dbContext .Albums .FirstOrDefault(a => a.Name == albumName); Tag tag = dbContext .Tags .FirstOrDefault(t => t.Name == $"#{tagName}"); if (album == null || tag == null) { throw new ArgumentException(Constants.AlbumOrTagDoNotExist); } if (Helper.IsAlbumTagExist(dbContext, albumName, tagName)) { throw new ArgumentException(Constants.AlbumOrTagDoNotExist); } AlbumRole albumRole = dbContext .AlbumRoles .FirstOrDefault(ar => ar.AlbumId == album.Id && ar.UserId == currentUser.Id); if (albumRole == null) { throw new ArgumentException(Constants.NotUploadPicture); } using (dbContext) { AlbumTag albumTag = new AlbumTag() { AlbumId = album.Id, TagId = tag.Id }; dbContext.AlbumTags.Add(albumTag); dbContext.SaveChanges(); } return(string.Format(Constants.AddTagToAlbum, tagName, albumName)); }
public AlbumTag AddTagTo(int albumId, int tagId) { var albumTag = new AlbumTag { AlbumId = albumId, TagId = tagId }; context.AlbumTags.Add(albumTag); context.SaveChanges(); return(albumTag); }
// AddTagTo <albumName> <tag> public static string Execute(string[] data) { string albumName = data[1]; string tagName = data[2].ValidateOrTransform(); using (PhotoShareContext context = new PhotoShareContext()) { var album = context.Albums .Include(x => x.AlbumTags) .Include(x => x.AlbumRoles) .ThenInclude(x => x.User) .Where(x => x.Name == albumName) .FirstOrDefault(); var tag = context.Tags .Where(x => x.Name == tagName) .FirstOrDefault(); if (album == null || tag == null) { throw new ArgumentException($"Either tag or album do not exist!"); } if (Session.User == null) { throw new InvalidOperationException($"Invalid credentials!"); } bool isOwner = album.AlbumRoles.Any(ar => ar.Role == Role.Owner && ar.User.Username == Session.User.Username); if (!isOwner) { throw new InvalidOperationException($"Invalid credentials!"); } AlbumTag at = new AlbumTag() { Album = album, Tag = tag }; context.AlbumTags.Add(at); context.SaveChanges(); } return($"Tag {tagName} added to {albumName}!"); }
public void AddTagToAlbum(string album, string tag) { Album alb = this.context.Albums.Single(a => a.Name == album); Tag t = this.context.Tags.Single(tn => tn.Name == tag); AlbumTag at = new AlbumTag() { AlbumId = alb.Id, TagId = t.Id }; this.context.AlbumTags.Add(at); this.context.SaveChanges(); }
public static int Save(AlbumTag item) { try { using (var db = new Entities(ConnectionStringHelper.ConnectionString())) { db.AlbumTag.Add(item); return(db.SaveChanges()); } } catch (EntityException e) { throw e; } }
public AlbumTag AddTagTo(int albumId, int tagId) { AlbumTag albumTag = new AlbumTag() { AlbumId = albumId, TagId = tagId }; if (!context.AlbumTags.Any(at => at.AlbumId == albumId && at.TagId == tagId)) { context.AlbumTags.Add(albumTag); } context.SaveChanges(); return(albumTag); }