Exemple #1
0
        private void CreateAndAddAudio(PlayCatDbContext context, Guid playlistId, int count)
        {
            if (count < -0)
            {
                return;
            }

            for (int i = 0; i < count; i++)
            {
                DataModel.Audio audio = context.CreateAudio(DateTime.Now.AddMinutes(i), "access" + i, "artist" + i, "song" + i, BaseAudioExtension, i.ToString(), i.ToString(), null);

                DataModel.AudioPlaylist audioPlaylist = context.CreateAudioPlaylist(DateTime.Now.AddMinutes(i), audio.Id, playlistId, i);
            }
        }
Exemple #2
0
        public BaseResult AddToPlaylist(Guid userId, AddRemovePlaylistRequest request)
        {
            return(BaseInvokeWithTransaction(() =>
            {
                //Is need check audioId??? - perfomance issue
                var playlistInfo =
                    (from p in _dbContext.Playlists.Include(x => x.AudioPlaylists)
                     where p.Id == request.PlaylistId && p.OwnerId == userId
                     select new
                {
                    Playlist = p,
                    IsAlreadyAdded = p.AudioPlaylists.Any(x => x.AudioId == request.AudioId),
                })
                    .FirstOrDefault();

                if (playlistInfo == null)
                {
                    return ResponseBuilder <BaseResult> .Fail().SetInfoAndBuild("Playlist not found");
                }

                if (playlistInfo.IsAlreadyAdded)
                {
                    return ResponseBuilder <BaseResult> .Fail().SetInfoAndBuild("Audio is already added");
                }

                var audioPlaylist = new DataModel.AudioPlaylist()
                {
                    AudioId = request.AudioId,
                    DateCreated = DateTime.Now,
                    Order = playlistInfo.Playlist.OrderValue,
                    PlaylistId = request.PlaylistId,
                };

                playlistInfo.Playlist.OrderValue++;
                _dbContext.Entry(playlistInfo.Playlist).State = EntityState.Modified;

                _dbContext.AudioPlaylists.Add(audioPlaylist);
                _dbContext.SaveChanges();

                return ResponseBuilder <BaseResult> .SuccessBuild();
            }));
        }
Exemple #3
0
        public async Task <UploadResult> UploadAudioAsync(Guid userId, UploadAudioRequest request)
        {
            return(await BaseInvokeCheckModelAsync(request, async() =>
            {
                DataModel.User user = _dbContext.Users.FirstOrDefault(x => x.Id == userId);
                if (user == null)
                {
                    throw new Exception("User not found, but token does");
                }

                var responseBuilder =
                    ResponseBuilder <UploadResult>
                    .Fail();

                if (user.IsUploadingAudio)
                {
                    return responseBuilder.SetInfoAndBuild("User already uploading audio");
                }

                user.IsUploadingAudio = true;
                _dbContext.SaveChanges();

                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        GetInfoResult result = GetInfo(new UrlRequest()
                        {
                            Url = request.Url
                        });

                        if (!result.Ok)
                        {
                            return responseBuilder
                            .SetErrors(result.Errors)
                            .SetInfoAndBuild(result.Info);
                        }

                        string videoId = UrlFormatter.GetYoutubeVideoIdentifier(request.Url);

                        IFile videoFile = _saveVideo.Save(request.Url);
                        IFile audioFile = await _extractAudio.ExtractAsync(videoFile);

                        //TODO: create upload for FileSystem, Blob, etc...
                        string accessUrl = _uploadAudio.Upload(audioFile, StorageType.FileSystem);

                        var generalPlayList = _dbContext.Playlists.FirstOrDefault(x => x.OwnerId == userId && x.IsGeneral);

                        if (generalPlayList == null)
                        {
                            throw new Exception("Playlist not found");
                        }

                        var audio = new DataModel.Audio()
                        {
                            Id = Guid.NewGuid(),
                            AccessUrl = accessUrl,
                            DateCreated = DateTime.Now,
                            Artist = request.Artist,
                            Song = request.Song,
                            Duration = audioFile.Duration,
                            Extension = audioFile.Extension,
                            FileName = audioFile.Filename,
                            UniqueIdentifier = videoId,
                            UploaderId = userId,
                        };

                        var audioPlaylist = new DataModel.AudioPlaylist()
                        {
                            AudioId = audio.Id,
                            DateCreated = DateTime.Now,
                            PlaylistId = generalPlayList.Id,
                            Order = generalPlayList.OrderValue,
                        };

                        //skip upload process
                        user.IsUploadingAudio = false;

                        //update max index in playlist
                        generalPlayList.OrderValue++;

                        //add entities
                        _dbContext.AudioPlaylists.Add(audioPlaylist);
                        DataModel.Audio audioEntity = _dbContext.Audios.Add(audio).Entity;

                        _dbContext.SaveChanges();

                        transaction.Commit();
                        return ResponseBuilder <UploadResult> .SuccessBuild(new UploadResult()
                        {
                            Audio = AudioMapper.ToApi.FromData(audioEntity),
                        });
                    } catch (Exception ex)
                    {
                        transaction.Rollback();

                        user.IsUploadingAudio = false;
                        _dbContext.SaveChanges();

                        throw ex;
                    }
                }
            }));
        }