Пример #1
0
        public async Task <IActionResult> AddSong(SongCreateDto model, int artistId)
        {
            if (ModelState.IsValid)
            {
                var result = await _songService.Create(model);

                if (result.IsSuccess)
                {
                    return(RedirectToAction("Album", new { artistId }));
                }

                throw new Exception("No se pudo registrar el álbum");
            }
            else
            {
                ModelState.AddModelError("SongCreation", "No se pudo registrar la canción.");
            }

            // Volvemos a pasar el modelo en caso falle para que cargue la data de nuevo
            var artist = await _artistService.Get(artistId);

            var albums = await _albumService.GetAllByArtist(artistId);

            var resultViewModel = new AlbumViewModel
            {
                ArtistId   = artist.ArtistId,
                ArtistName = artist.Name,
                Albums     = albums
            };

            // Especificamos la ruta de la vista manualmente
            return(View("Album", resultViewModel));
        }
Пример #2
0
        public async Task <bool> UpdateSong(Guid id, SongCreateDto songCreateDto)
        {
            var song = await _db.Songs.FirstOrDefaultAsync(c => c.SongId == id);

            if (song == null)
            {
                return(false);
            }


            var performer = await _db.Performers.FirstOrDefaultAsync(c => c.PerformerId == songCreateDto.PerformerIdDto);

            if (performer == null)
            {
                return(false);
            }


            song = await songCreateDto.ToEntity();

            await _db.SaveChangesAsync();

            await _db.DisposeAsync();

            return(true);
        }
Пример #3
0
        public ActionResult <SongReadDto> CreateSong(SongCreateDto songCreateDto)
        {
            var songModel = _mapper.Map <Song>(songCreateDto);

            _repository.CreateSong(songModel);
            _repository.SaveChanges();

            var songReadDto = _mapper.Map <SongReadDto>(songModel);

            return(CreatedAtRoute(nameof(GetSongById), new { Id = songReadDto.Id }, songReadDto));
        }
Пример #4
0
        public IActionResult AddSong([FromBody] SongCreateDto songCreateDto)
        {
            Song toAdd = Mapper.Map <Song>(songCreateDto);

            _songRepository.Add(toAdd);

            bool result = _songRepository.Save();

            if (!result)
            {
                return(new StatusCodeResult(500));
            }

            return(CreatedAtRoute("GetSingleSong", new { id = toAdd.ID }, Mapper.Map <SongDto>(toAdd)));
        }
Пример #5
0
        public async Task <ResponseHelper> Create(SongCreateDto model)
        {
            var result = new ResponseHelper();

            try
            {
                var entry = Mapper.Map <Song>(model);
                entry.DurationTime = TimeSpan.FromSeconds(model.DurationTime);

                await _dataContext.AddAsync(entry);

                await _dataContext.SaveChangesAsync();

                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }
            return(result);
        }
Пример #6
0
        public async Task <ActionResult> AddSong([FromBody] SongCreateDto songCreateDto)
        {
            var isAdded = await _songs.AddSong(songCreateDto);

            return(isAdded ? Ok() : NotFound());   //как сделать так чтобы указать что не найден исполнитель
        }
Пример #7
0
        public async Task <ActionResult> UpdateSongInf(Guid id, [FromBody] SongCreateDto songCreateDto)
        {
            var IsUpdated = await _songs.UpdateSong(id, songCreateDto);

            return(IsUpdated ? Ok() : NotFound());
        }