Exemplo n.º 1
0
        public async Task <ActionResult <PodcastEntryViewModel> > Post([FromBody] PodcastEntryViewModel item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid podcast entry posted"));
            }
            PodcastEntry entry;

            if (item.Id != null)
            {
                entry = await _repository.GetAsync(item.Id);

                _mapper.Map(item, entry);
            }
            else
            {
                entry = _mapper.Map <PodcastEntryViewModel, PodcastEntry>(item);
            }

            if (entry is null)
            {
                return(BadRequest());
            }

            if (entry.ProcessingStatus == ProcessingStatus.Uploading ||
                entry.ProcessingStatus == ProcessingStatus.Processed)
            {
                // we're editing an existing entry
                await _repository.AddOrUpdateWithTags(entry);

                await _unitOfWork.CompleteAsync();

                var result = _mapper.Map <PodcastEntry, PodcastEntryViewModel>(entry);
                return(Ok(result));
            }

            //we're adding a new entry
            //TODO: This should return the properties bundle
            //with the status as a member
            var parser  = new UrlTypeParser();
            var urlType = await parser.GetUrlType(entry.SourceUrl);

            if (!urlType.Equals(UrlType.Invalid) &&
                !urlType.Equals(UrlType.Playlist) &&
                !urlType.Equals(UrlType.Channel))
            {
                // check user quota
                var result = await _preProcessor.PreProcessEntry(
                    _applicationUser,
                    entry);

                switch (result)
                {
                case EntryProcessResult.QuotaExceeded:
                    return(StatusCode(402));

                case EntryProcessResult.GeneralFailure:
                    return(BadRequest());
                }

                _repository.GetContext()
                .Entry(entry)
                .State = EntityState.Detached;

                entry = await _repository.GetAsync(entry.Id);

                return(_mapper.Map <PodcastEntry, PodcastEntryViewModel>(entry));
            }

            if (urlType.Equals(UrlType.Playlist))
            {
                entry.ProcessingStatus = ProcessingStatus.Deferred;
                var result = _mapper.Map <PodcastEntry, PodcastEntryViewModel>(entry);
                return(Accepted(result));
            }

            return(BadRequest("Failed to create podcast entry"));
        }