public IHttpActionResult CreateNewImageAlbum([FromBody] ImageAlbumCreateBindingModel model) { if (model == null) { return this.BadRequest("No data."); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var user = this.Data.Users.Find(this.User.Identity.GetUserId()); var imageAlbum = new ImageAlbum() { Title = model.Title, CreatedById = user.Id, DateCreated = DateTime.Now }; if (this.Data.ImageAlbums.All().Any(a => a.CreatedById == user.Id && a.Title == imageAlbum.Title)) { return this.BadRequest(string.Format("A image album with the specified title already exists.")); } this.Data.ImageAlbums.Add(imageAlbum); this.Data.SaveChanges(); var userFollowers = user.FollowedBy.Select(u => u.Id); foreach (var userId in userFollowers) { var notification = new Notification() { ImageAlbumId = imageAlbum.Id, RecipientId = userId, NotificationType = NotificationType.CreatedImageAlbum, DateCreated = DateTime.Now, Message = user.UserName + " created image album " + imageAlbum.Title + "." }; this.Data.Notifications.Add(notification); } this.Data.SaveChanges(); var returnItem = Mapper.Map<ImageAlbumViewModel>(imageAlbum); returnItem.IsMine = true; return this.Ok(returnItem); }
public IHttpActionResult AddMusicAlbum([FromBody] MusicAlbumBindingModel musicAlbum) { if (musicAlbum == null) { return this.BadRequest("Cannot create an empty music album model."); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (musicAlbum.CoverImageData != null && musicAlbum.CoverImageData.IndexOf(',') == -1) { musicAlbum.CoverImageData = string.Format("{0}{1}", "data:image/jpg;base64,", musicAlbum.CoverImageData); } var user = this.Data.Users.Find(this.User.Identity.GetUserId()); var newMusicAlbum = new MusicAlbum { Title = musicAlbum.Title, AuthorId = user.Id, IsPublic = musicAlbum.IsPublic, DateCreated = DateTime.Now, CoverImageData = musicAlbum.CoverImageData }; if ( this.Data.MusicAlbums.All() .Any(a => a.Title == newMusicAlbum.Title && a.AuthorId == newMusicAlbum.AuthorId)) { return this.BadRequest("This music album already exists."); } this.Data.MusicAlbums.Add(newMusicAlbum); this.Data.SaveChanges(); var userFollowers = user.FollowedBy.Select(u => u.Id); foreach (var userId in userFollowers) { var notification = new Notification() { MusicAlbumId = newMusicAlbum.Id, RecipientId = userId, NotificationType = NotificationType.CreatedMusicAlbum, DateCreated = DateTime.Now, Message = user.UserName + " created music album." }; this.Data.Notifications.Add(notification); } this.Data.SaveChanges(); var musicAlbumToReturn = Mapper.Map<MusicAlbumViewModel>(newMusicAlbum); return this.Ok(musicAlbumToReturn); }
public IHttpActionResult CreateNewImage([FromBody] ImageCreateBindingModel model) { if (model == null) { return this.BadRequest(); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (!this.Data.ImageAlbums.All().Any(a => a.Id == model.ImageAlbumId)) { return this.BadRequest("There is no image album with this id."); } var imageAlbum = this.Data.ImageAlbums.Find(model.ImageAlbumId); var user = this.Data.Users.Find(this.User.Identity.GetUserId()); var image = new Image() { Title = model.Title, UploadedById = user.Id, DateCreated = DateTime.Now, ImageAlbumId = imageAlbum.Id, ImageData = model.Base64ImageString }; this.Data.Images.Add(image); this.Data.SaveChanges(); var userFollowers = user.FollowedBy.Select(u => u.Id); var albumFollowers = imageAlbum.Followers.Where(u => !userFollowers.Contains(u.Id)) .Select(u => u.Id); foreach (var userId in userFollowers) { var notification = new Notification() { ImageId = image.Id, RecipientId = userId, NotificationType = NotificationType.AddedImage, DateCreated = DateTime.Now, Message = user.UserName + " created image." }; this.Data.Notifications.Add(notification); } foreach (var userId in albumFollowers) { var notification = new Notification() { ImageId = image.Id, RecipientId = userId, NotificationType = NotificationType.AddedImage, DateCreated = DateTime.Now, Message = imageAlbum.Title + " new image." }; this.Data.Notifications.Add(notification); } this.Data.SaveChanges(); var returnItem = Mapper.Map<ImageViewModel>(image); returnItem.IsMine = true; return Ok(returnItem); }
public IHttpActionResult AddSong([FromUri] int id, [FromBody] SongBindingModel song) { var album = this.Data.MusicAlbums.Find(id); if (album == null) { return this.NotFound(); } if (song == null) { return this.BadRequest("Cannot create an empty song model."); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var user = this.Data.Users.Find(this.User.Identity.GetUserId()); if (user.Id != album.AuthorId) { return this.Unauthorized(); } if (!this.ValidateAudioFileType(song.FileDataUrl)) { return this.BadRequest("Invalid file type. Valid file type includes .mp3 only"); } if (!this.ValidateFileSize(song.FileDataUrl, SongKilobyteLimit)) { return this.BadRequest(string.Format("Song size should be less than {0} kB.", SongKilobyteLimit)); } var metadataStart = song.FileDataUrl.IndexOf("data:audio/mp3;base64,", StringComparison.Ordinal); if (metadataStart != -1) { song.FileDataUrl = song.FileDataUrl.Remove(metadataStart, metadataStart + 22); } string googleDriveFileName = song.Artist + " - " + song.Title + ".mp3"; string[] uploadResults = this.UploadSongToGoogleDrive(song.FileDataUrl, googleDriveFileName); if (uploadResults[0] == "success") { song.FileDataUrl = uploadResults[1]; var newSong = new Song { Title = song.Title, Artist = song.Artist, FilePath = song.FileDataUrl, MusicAlbumId = album.Id, UploaderId = album.AuthorId, DateAdded = DateTime.Now, TrackNumber = song.TrackNumber == null ? (int?)null : int.Parse(song.TrackNumber), OriginalAlbumTitle = song.OriginalAlbumTitle, OriginalAlbumArtist = song.OriginalAlbumArtist, OriginalDate = song.OriginalDate == null ? (DateTime?)null : DateTime.Parse(song.OriginalDate), Genre = song.Genre, Composer = song.Composer, Publisher = song.Publisher, Bpm = song.Bpm == null ? (int?)null : int.Parse(song.Bpm) }; if (album.Songs.Contains(newSong)) { return this.BadRequest("This song already exists in album."); } this.Data.Songs.Add(newSong); this.Data.SaveChanges(); var userFollowers = user.FollowedBy.Select(u => u.Id); var albumFollowers = album.Followers.Where(u => !userFollowers.Contains(u.Id)).Select(u => u.Id); foreach (var userId in userFollowers) { var notification = new Notification() { SongId = newSong.Id, RecipientId = userId, NotificationType = NotificationType.AddedSong, DateCreated = DateTime.Now, Message = user.UserName + " added song." }; this.Data.Notifications.Add(notification); } foreach (var userId in albumFollowers) { var notification = new Notification() { SongId = newSong.Id, RecipientId = userId, NotificationType = NotificationType.AddedSong, DateCreated = DateTime.Now, Message = album.Title + " new song." }; this.Data.Notifications.Add(notification); } this.Data.SaveChanges(); var songToReturn = Mapper.Map<SongViewModel>(newSong); return this.Ok(songToReturn); } else { return this.BadRequest(uploadResults[1]); } }