コード例 #1
0
        public async Task <IActionResult> Create([FromBody] GenreCreateDTO genreDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Started Create Genre");
                if (genreDTO == null)
                {
                    _logger.LogError($"{location}: Empty request");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError($"{location}: Wrong model state");
                    return(BadRequest(ModelState));
                }
                var genre     = _mapper.Map <Genre>(genreDTO);
                var IsSuccess = await _genreRepository.Create(genre);

                if (!IsSuccess)
                {
                    return(InternalError($"{location} : Creation failed"));
                }
                return(Created("Create", new { genre }));
            }
            catch (Exception ex)
            {
                return(InternalError($"{location} : Something wrong {ex.Message}"));
            }
        }
コード例 #2
0
        public async Task <GenreDTO> PutAsync(GenreCreateDTO genre)
        {
            this.Logger.LogTrace($"{nameof(this.PutAsync)} called");

            var result = await this.GenreCreateService.CreateAsync(this.Mapper.Map <GenreUpdateModel>(genre));

            return(this.Mapper.Map <GenreDTO>(result));
        }
コード例 #3
0
        public IActionResult AddNewGenre(GenreCreateDTO genre)
        {
            var guid = genreService.AddNewGenre(genre);

            string location = linkGenerator.GetPathByAction("GetGenreByID", "Genre", new { genreID = guid });

            return(Created(location, guid));
        }
コード例 #4
0
        public async Task <ActionResult> Put(int id, [FromBody] GenreCreateDTO model)
        {
            try
            {
                var entity = _automapper.Map <Genre>(model);
                entity.Id = id;
                _db.Entry(entity).State = EntityState.Modified;
                await _db.SaveChangesAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
コード例 #5
0
        public async Task <ActionResult> Post([FromBody] GenreCreateDTO model)
        {
            try
            {
                var entity = _automapper.Map <Genre>(model);
                await _db.AddAsync(entity);

                await _db.SaveChangesAsync();

                var genreDTO = _automapper.Map <GenreDTO>(entity);

                return(new CreatedAtRouteResult("GetGenreById", new { id = genreDTO.Id }, genreDTO));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
コード例 #6
0
 public Guid AddNewGenre(GenreCreateDTO genre)
 {
     return(genreRepository.AddNewGenre(mapper.Map <Genre>(genre)));
 }