Пример #1
0
        public IActionResult CreatePlaylist([FromBody] PlaylistForCreationDto playlist)
        {
            try
            {
                if (playlist == null)
                {
                    _logger.LogError("Playlist object sent from the client is null.");
                    return(BadRequest("Playist object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid playlist object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var playlistEntity = _mapper.Map <Playlist>(playlist);

                _repoWrapper.Playlist.CreatePlaylist(playlistEntity);
                _repoWrapper.Save();

                var createdPlaylist = _mapper.Map <PlaylistDto>(playlistEntity);

                return(CreatedAtRoute("PlaylistById", new { id = createdPlaylist.Id }, createdPlaylist));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreatePlaylist action: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Пример #2
0
        public async Task <IActionResult> CreatePlaylist(int userId, [FromBody] PlaylistForCreationDto playlistForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var creator = await _repo.GetUser(userId);

            if (creator == null)
            {
                return(NotFound("User not found"));
            }
            playlistForCreationDto.CreatorId = userId;

            var playlist = _mapper.Map <Playlist>(playlistForCreationDto);

            _repo.Add(playlist);

            if (await _repo.SaveAll())
            {
                var returnPlaylist = _mapper.Map <PlaylistToReturn>(playlist);
                return(CreatedAtRoute("GetPlaylist", new { id = playlist.PlaylistId }, returnPlaylist));
            }

            throw new Exception("Creating of playlist failed on save");
        }
Пример #3
0
        public async Task <IActionResult> Addplaylist(int id, [FromBody] PlaylistForCreationDto playlistForCreationDto)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            playlistForCreationDto.UserId = id;
            var playlist = _mapper.Map <playlist>(playlistForCreationDto);

            _repo.Add(playlist);
            if (await _repo.SaveAll())
            {
                var playlistToReturn = _mapper.Map <PlaylistToReturnDto>(playlist);
                return(CreatedAtRoute("GetPlaylist", new { id = playlist.Id }, playlistToReturn));
            }
            throw new Exception("Field To Create Post");
        }