コード例 #1
0
        public async Task <IActionResult> CreateAlbumAsync([FromBody] AlbumViewModel albumViewModel)
        {
            _logger?.LogDebug("'{0}' has been invoked", MethodBase.GetCurrentMethod().DeclaringType);
            var response = new AlbumViewModel();

            if (ModelState.IsValid)
            {
                try
                {
                    _logger?.LogInformation("{0} has been retrieved successfully.", MethodBase.GetCurrentMethod().Name);

                    var album = await _albumService.CreateAlbumAsync(_mapper.Map <AlbumDto>(albumViewModel));

                    response = _mapper.Map <AlbumViewModel>(album);
                }

                catch (Exception ex)
                {
                    _logger?.LogCritical("There was an error on '{0}' invocation: {1}", MethodBase.GetCurrentMethod().DeclaringType, ex);
                    response = new AlbumViewModel {
                        ErrorMessage = new string[] { ex.Message }
                    };
                }
            }
            else
            {
                response = new AlbumViewModel {
                    ErrorMessage = new string[] { "ModelState is not valid: " + ModelState.ToString() }
                }
            };

            return(Ok(response));
        }
コード例 #2
0
        public async Task <ActionResult <AlbumModel> > CreateAsync([FromBody] AlbumModel model)
        {
            try
            {
                AlbumModel createdAlbum = await albumService.CreateAlbumAsync(model);

                return(Ok(createdAlbum));
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }