Ejemplo n.º 1
0
        public async Task <ActionResult <MusicResource> > UpdateMusic(int id, [FromBody] SaveMusicResource saveMusicResource)
        {
            var validator        = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusicResource);

            var requestIdValidId = id == 0 || !validationResult.IsValid;

            if (requestIdValidId)
            {
                return(BadRequest(validationResult.Errors));
            }

            var musicToUpdate = await _musicService.GetMusicById(id);

            if (musicToUpdate == null)
            {
                return(null);
            }

            var music = _mapper.Map <SaveMusicResource, Music>(saveMusicResource);

            await _musicService.UpdateMusic(musicToUpdate, music);

            var updatedMusic = await _musicService.GetMusicById(id);

            var UpdatedMusicResource = _mapper.Map <Music, MusicResource>(updatedMusic);

            return(Ok(UpdatedMusicResource));
        }
Ejemplo n.º 2
0
        // [Authorize]
        public async Task <ActionResult <MusicResource> > CreateMusic(SaveMusicResource saveMusicResource)
        {
            try
            {
                //GET Current user
                var userId = User.Identity.Name;
                // Validation
                SaveMusicResourceValidator validation       = new SaveMusicResourceValidator();
                ValidationResult           validationResult = await validation.ValidateAsync(saveMusicResource);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(validationResult.Errors));
                }
                // mappage view à la BD
                Music music = _mapperService.Map <SaveMusicResource, Music>(saveMusicResource);
                // Creation de music
                Music newMusic = await _musicService.CreateMusic(music);

                // mappage BD à la view
                MusicResource musicResource = _mapperService.Map <Music, MusicResource>(newMusic);
                return(Ok(musicResource));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <MusicDTO> > UpdateMusic(int id, [FromBody] SaveMusicDTO saveMusicResource)
        {
            var validator        = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusicResource);

            if (!validationResult.IsValid || id == 0)
            {
                return(BadRequest(validationResult.Errors));
            }

            var musicToBeUpdate = await _musicService.GetMusicById(id);

            if (musicToBeUpdate == null)
            {
                return(NotFound());
            }

            var music = _mapper.Map <SaveMusicDTO, Music>(saveMusicResource);

            await _musicService.UpdateMusic(musicToBeUpdate, music);

            var updateMusic = await _musicService.GetMusicById(id);

            var updateMusicDTO = _mapper.Map <Music, MusicDTO>(updateMusic);

            return(Ok(updateMusicDTO));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <MusicResource> > UpdateMusic(int id, [FromBody] SaveResourceMusic updateSaveResource)
        {
            var validator      = new SaveMusicResourceValidator();
            var resultValidato = await validator.ValidateAsync(updateSaveResource);

            if (!resultValidato.IsValid)
            {
                return(BadRequest(resultValidato.Errors));
            }

            var musicToBeUpdate = await _musicService.GetMusicById(id);

            if (musicToBeUpdate == null)
            {
                return(NotFound());
            }

            var musicUpadte = _mapper.Map <SaveResourceMusic, Music>(updateSaveResource);
            await _musicService.UpdateMusic(musicToBeUpdate, musicUpadte);

            var musicNewUpdate = await _musicService.GetMusicById(id);

            var musicUpdateResource = _mapper.Map <Music, MusicResource>(musicNewUpdate);

            return(Ok(musicUpdateResource));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <MusicResource> > UpdateMusic(int id, SaveMusicResource saveMusicResource)
        {
            try
            {
                var validation       = new SaveMusicResourceValidator();
                var validationResult = await validation.ValidateAsync(saveMusicResource);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(validationResult.Errors));
                }

                var musicToBeUpdated = await _musicService.GetMusicById(id);

                if (musicToBeUpdated == null)
                {
                    return(BadRequest("Cannot update this music because it doesn't exist"));
                }

                var music = _mapperService.Map <SaveMusicResource, Music>(saveMusicResource);

                // Update in DB
                var musicUpdated = await _musicService.UpdateMusic(musicToBeUpdated, music);

                var musicResourceUpdated = _mapperService.Map <Music, MusicResource>(musicUpdated);
                return(Ok(musicResourceUpdated));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 6
0
    public async Task <ActionResult <MusicResource> > UpdateMusic(int id, [FromBody] SaveMusicResource saveMusicResource)
    {
        var validator        = new SaveMusicResourceValidator();
        var validationResult = await validator.ValidateAsync(saveMusicResource);

        var requestIsInvalid = id == 0 || !validationResult.IsValid;

        if (requestIsInvalid)
        {
            return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
        }
        var musicToBeUpdate = await _musicService.GetMusicById(id);

        if (musicToBeUpdate == null)
        {
            return(NotFound());
        }

        var music = _mapper.Map <SaveMusicResource, Music>(saveMusicResource);

        await _musicService.UpdateMusic(musicToBeUpdate, music);

        var updatedMusic = await _musicService.GetMusicById(id);

        var updatedMusicResource = _mapper.Map <Music, MusicResource>(updatedMusic);

        return(Ok(updatedMusicResource));
    }
        public async Task <ActionResult <MusicResource> > CreateMusic([FromBody] SaveMusicResource saveMusicResource)
        {
            var validatorMusic   = new SaveMusicResourceValidator();
            var validationResult = await validatorMusic.ValidateAsync(saveMusicResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors.FirstOrDefault().ErrorMessage));
            }

            var artist = await _artistService.GetById(saveMusicResource.ArtistId);

            if (artist == null)
            {
                return(BadRequest("No such an artist"));
            }

            var music = _mapper.Map <SaveMusicResource, Music>(saveMusicResource);

            var newMusic = await _musicService.Create(music);

            var newMusicResource = _mapper.Map <Music, MusicResource>(newMusic);

            return(Ok(newMusicResource));
        }
        public async Task <ActionResult <MusicResource> > UpdateMusic(int id, [FromBody] SaveMusicResource saveMusicResource) // "Id is coming from the request url and saveMusicResource is coming from the request body".
        {
            var validator        = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusicResource);

            var requestIsInvalid = id == 0 || !validationResult.IsValid;

            if (requestIsInvalid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok. How could/should this be refined for production?
            }
            var musicToBeUpdated = await _musicService.GetMusicById(id);

            if (musicToBeUpdated == null)
            {
                return(NotFound());
            }

            var music = _mapper.Map <SaveMusicResource, Music>(saveMusicResource);

            await _musicService.UpdateMusic(musicToBeUpdated, music);

            var updatedMusic = await _musicService.GetMusicById(id);

            var updatedMusicResource = _mapper.Map <Music, MusicResource>(updatedMusic);

            return(Ok(updatedMusicResource));
        }
        public async Task <ActionResult <MusicResource> > UpdateMusic(int musicId, [FromBody] SaveMusicResource saveMusicResource)
        {
            var validatorMusic   = new SaveMusicResourceValidator();
            var validationResult = await validatorMusic.ValidateAsync(saveMusicResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var music = _mapper.Map <SaveMusicResource, Music>(saveMusicResource);

            var musicToBeUpdated = await _musicService.GetById(musicId);

            if (musicToBeUpdated == null)
            {
                return(NotFound());
            }

            var musicUpdated = await _musicService.Update(musicToBeUpdated, music);

            var musicUpdatedResource = _mapper.Map <Music, MusicResource>(musicUpdated);

            return(Ok(musicUpdatedResource));
        }
Ejemplo n.º 10
0
        [Authorize] // We need to be authenticate to create a music
        public async Task <ActionResult <MusicResource> > CreateMusic(SaveMusicResource saveMusicResource)
        {
            try
            {
                // Validation
                var validation       = new SaveMusicResourceValidator();
                var validationResult = await validation.ValidateAsync(saveMusicResource);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(validationResult.Errors));
                }

                // Mappage
                var music = _mapperService.Map <SaveMusicResource, Music>(saveMusicResource);

                // Music Creation
                var newMusic = await _musicService.CreateMusic(music);

                // Mappage
                var musicResource = _mapperService.Map <Music, MusicResource>(newMusic);
                return(Ok(musicResource));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 11
0
        public async Task <ActionResult <MusicResource> > UpdateMusic(int id, [FromBody] SaveMusicResource saveMusicResource)
        {
            SaveMusicResourceValidator validator = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusicResource);

            bool requestIsInvalid = id == 0 || !validationResult.IsValid;

            if (requestIsInvalid)
            {
                return(BadRequest(validationResult.Errors));
            }

            Music musicToBeUpdated = await _musicService.GetMusicById(id);

            if (musicToBeUpdated == null)
            {
                return(NotFound());
            }

            Music music = _mapper.Map <SaveMusicResource, Music>(saveMusicResource);
            await _musicService.UpdateMusic(musicToBeUpdated, music);

            var updateMusic = await _musicService.GetMusicById(id);

            var updateMusicResource = _mapper.Map <Music, MusicResource>(updateMusic);

            return(Ok(updateMusicResource));
        }
Ejemplo n.º 12
0
        public async Task <ActionResult <MusicResource> > CreateMusic([FromBody] SaveResourceMusic musicSaveResource)
        {
            var validatorMusic   = new SaveMusicResourceValidator();
            var validationResult = await validatorMusic.ValidateAsync(musicSaveResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }
            var music    = _mapper.Map <SaveResourceMusic, Music>(musicSaveResource);
            var newMusic = await _musicService.CreateMusic(music);

            return(Ok(newMusic));
        }
Ejemplo n.º 13
0
    public async Task <ActionResult <MusicResource> > CreateMusic([FromBody] SaveMusicResource saveMusicResource)
    {
        var validator        = new SaveMusicResourceValidator();
        var validationResult = await validator.ValidateAsync(saveMusicResource);

        if (!validationResult.IsValid)
        {
            return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
        }
        var musicToCreate = _mapper.Map <SaveMusicResource, Music>(saveMusicResource);

        var newMusic = await _musicService.CreateMusic(musicToCreate);

        var music = await _musicService.GetMusicById(newMusic.Id);

        var musicResource = _mapper.Map <Music, MusicResource>(music);

        return(Ok(musicResource));
    }
Ejemplo n.º 14
0
        public async Task <ActionResult <MusicDTO> > CreateMusic([FromBody] SaveMusicDTO saveMusicResource)
        {
            var validator        = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusicResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var musicToCreate = _mapper.Map <SaveMusicDTO, Music>(saveMusicResource);
            var newMusic      = await _musicService.CreateMusic(musicToCreate);

            var music = await _musicService.GetMusicWithArtistById(newMusic.Id);

            var musicResource = _mapper.Map <Music, MusicDTO>(music);

            return(Ok(musicResource));
        }
        public async Task <ActionResult <MusicResource> > CreateMusic([FromBody] SaveMusicResource saveMusicResource) // "Notice the [FromBody] attribute before the parameter, it indicates that this object is coming from the requests body".
        {
            var validator        = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusicResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok. How could/should this be refined for production?
            }
            var musicToCreate = _mapper.Map <SaveMusicResource, Music>(saveMusicResource);

            var newMusic = await _musicService.CreateMusic(musicToCreate);

            var music = await _musicService.GetMusicById(newMusic.Id);

            var musicResource = _mapper.Map <Music, MusicResource>(music);

            return(Ok(musicResource));
        }
Ejemplo n.º 16
0
        public async Task <ActionResult <MusicResource> > CreateMusic([FromBody] SaveMusicResource saveMusicResource)
        {
            SaveMusicResourceValidator validator = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusicResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            Music musicToBeCreated = _mapper.Map <SaveMusicResource, Music>(saveMusicResource);
            Music newMusic         = await _musicService.CreateMusic(musicToBeCreated);

            Music music = await _musicService.GetMusicById(newMusic.Id);

            var musicResource = _mapper.Map <Music, MusicResource>(music);

            return(Ok(musicResource));
        }
Ejemplo n.º 17
0
        public async Task <ActionResult <MusicResource> > UpdateMusic(int id, [FromBody] SaveMusicResource saveMusic)
        {
            var validator        = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusic);

            var errorResponse = new ErrorResponse <IList <FluentValidation.Results.ValidationFailure> >();

            if (!validationResult.IsValid)
            {
                errorResponse.status  = 400;
                errorResponse.errors  = validationResult.Errors;
                errorResponse.message = "Bad Request";
                return(BadRequest(errorResponse));
            }

            var savedMusicFromDb = await _musicService.GetMusicById(id);

            var response = new Response <MusicResource>();

            if (savedMusicFromDb == null)
            {
                response.status  = 404;
                response.message = string.Format("Music with id: {0} does not exist", id);

                return(StatusCode(404, response));
            }

            var mappedSavedMusic = _mapper.Map <SaveMusicResource, Music>(saveMusic);
            await _musicService.UpdateMusic(savedMusicFromDb, mappedSavedMusic);

            var musicWithArtist = await _musicService.GetMusicById(savedMusicFromDb.Id);

            var musicResource = _mapper.Map <Music, MusicResource>(musicWithArtist);

            response.data    = musicResource;
            response.status  = 200;
            response.message = "Music Updated Successfully";

            return(Ok(response));
        }
Ejemplo n.º 18
0
        public async Task <ActionResult <Response <MusicResource> > > CreateMusic([FromBody] SaveMusicResource saveMusic)
        {
            var validator       = new SaveMusicResourceValidator();
            var validatorResult = await validator.ValidateAsync(saveMusic);

            var errorResponse = new ErrorResponse <IList <FluentValidation.Results.ValidationFailure> >();

            if (!validatorResult.IsValid)
            {
                errorResponse.status  = 400;
                errorResponse.errors  = validatorResult.Errors;
                errorResponse.message = "Bad Request";
                return(BadRequest(errorResponse));
            }

            var music      = _mapper.Map <SaveMusicResource, Music>(saveMusic);
            var savedMusic = await _musicService.CreateMusic(music);

            var musicWithArtist = await _musicService.GetMusicById(savedMusic.Id);

            var response = new Response <MusicResource>();

            if (musicWithArtist == null)
            {
                response.status  = 500;
                response.message = "Failed to save. An error occurred";

                return(StatusCode(500, response));
            }

            var musicResource = _mapper.Map <Music, MusicResource>(musicWithArtist);

            response.data    = musicResource;
            response.status  = 201;
            response.message = "Music Created Successfully";

            return(CreatedAtAction(nameof(GetByMusicId), new { id = musicResource.Id }, response));
        }
Ejemplo n.º 19
0
        public async Task <ActionResult <MusicResource> > UpdateMusic(int id, SaveMusicResource saveMusicResource)
        {
            try
            {
                // si la music existe depuis le id
                Music musicUpdate = await _musicService.GetMusicById(id);

                if (musicUpdate == null)
                {
                    return(BadRequest("la music n'existe pas "));
                }

                /// validation
                SaveMusicResourceValidator validation       = new SaveMusicResourceValidator();
                ValidationResult           validationResult = await validation.ValidateAsync(saveMusicResource);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(validationResult.Errors));
                }

                //mappage view to db
                Music music = _mapperService.Map <SaveMusicResource, Music>(saveMusicResource);
                //upadate dans la bd
                await _musicService.UpdateMusic(musicUpdate, music);

                //get the updated music
                Music musicUpdateNew = await _musicService.GetMusicById(id);

                //mappage bd to view
                SaveMusicResource musicResourceUpdate = _mapperService.Map <Music, SaveMusicResource>(musicUpdateNew);
                return(Ok(musicResourceUpdate));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }