Beispiel #1
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));
            }
        }
Beispiel #2
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));
        }
Beispiel #3
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));
        }
Beispiel #4
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> > 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 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));
            }
        }
        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));
        }
        [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));
            }
        }
        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> > CreateMusic(SaveMusicResource saveMusicResource)
        {
            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));
        }
Beispiel #11
0
        public async Task <ActionResult <MusicResource> > CreateMusic([FromBody] SaveMusicResource saveMusicResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState)); // this needs refining, but for demo it is ok
            }
            var musicToCreate = _mapper.Map <Music>(saveMusicResource);

            var newMusic = await _musicService.CreateMusic(musicToCreate);

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

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

            return(Ok(musicResource));
        }
Beispiel #12
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));
            }

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

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

            return(Ok(musicResource));
        }
        public async Task <ActionResult <MusicResource> > UpdateMusic(int id, SaveMusicResource saveMusicResource)
        {
            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));
        }
Beispiel #14
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));
    }
        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));
        }
Beispiel #16
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));
        }
Beispiel #17
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));
            }
        }
Beispiel #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));
        }
Beispiel #19
0
        public async Task MusicsController_CreateMusic_ReturnCreated()
        {
            var options = new DbContextOptionsBuilder <MyMusicDbContext>()
                          .UseInMemoryDatabase(databaseName: "TestNewListDb").Options;
            IActionResult result;

            using (var context = new MyMusicDbContext(options))
            {
                _UnitOfWork   = new UnitOfWork(context);
                _musicService = new MusicService(_UnitOfWork);



                var saveMusicResource = new SaveMusicResource()
                {
                    Name     = "Music Name1",
                    ArtistId = 1,
                };
                var controller = new MusicsController(_musicService, _mapper);



                result = controller.CreateMusic(saveMusicResource)  as  IActionResult;

                var createResult  = Assert.IsType <CreatedAtActionResult>(result);
                var musicResourse = Assert.IsType <MusicResource>(createResult.Value);

                Assert.IsType <CreatedAtActionResult>(createResult);
                Assert.IsType <MusicResource>(musicResourse);



                Assert.Equal(1, musicResourse.Id);
                Assert.Equal("Music Name1", musicResourse.Name);
            };
        }