Ejemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] CreateAuthorDTO createAuthorDTO)
        {
            try
            {
                if (createAuthorDTO == null)
                {
                    _logger.LogWarn("Bed request from Create0");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn("Bed request from Create1");
                    return(BadRequest(ModelState));
                }
                var record    = _mapper.Map <Author>(createAuthorDTO);
                var isSuccess = await _authorRepository.Create(record);

                if (!isSuccess)
                {
                    _logger.LogWarn("Create in db goes wrong");
                    return(StatusCode(500, "Internal Error"));
                }
                return(Created("Created", new { record }));
            }
            catch (Exception e)
            {
                _logger.LogInfo($"{e.Message} - {e.InnerException}");
                return(StatusCode(500, "Internal Error"));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Login([FromBody] UserDTO userDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                var username = userDTO.Username;
                var password = userDTO.Password;
                _logger.LogInfo($"{location}: Login attempt from user {username}");
                var result = await _signInManager.PasswordSignInAsync(username, password, false, false);


                if (result.Succeeded)
                {
                    _logger.LogInfo($"{location}: {username} Succesfully authenticated");
                    var user = await _userManager.FindByNameAsync(username);

                    var tokenString = await GenerateJSONWebtoken(user);

                    return(Ok(new { token = tokenString }));
                }
                _logger.LogWarn($"{location}: {username} Not authenticated");
                return(Unauthorized(userDTO));
            }
            catch (Exception e)
            {
                return(InternalError($"{ e.Message} - { e.InnerException }"));
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> GetAuthor(int id)
        {
            try
            {
                _logger.LogInfo("Attempted to Get Author with id:");
                var author = await _authorRepository.FindById(id);

                if (author == null)
                {
                    _logger.LogWarn($"author with id:{id} was not found");
                    return(NotFound());
                }
                var response = _mapper.Map <AuthorDTO>(author);
                return(Ok(response));
            }
            catch (Exception e)
            {
                return(InternalError($"{ e.Message} - { e.InnerException }"));
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> GetBook(int id)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Attempted Call for id: {id}");
                var book = await _bookRepository.FindById(id);

                if (book == null)
                {
                    _logger.LogWarn($"{location}: Failed to retrieve record with id: {id}");
                    return(NotFound());
                }
                var response = _mapper.Map <BookDTO>(book);
                _logger.LogInfo($"{location}: Succesfully got record with id: {id}");
                return(Ok(response));
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} -{e.InnerException}"));
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create([FromBody] CreateBookDTO createBookDTO)
        {
            try
            {
                if (createBookDTO == null)
                {
                    _logger.LogWarn($"{ GetCallLocation()}:Bed request from Create0");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{ GetCallLocation()}:Bed request from Create1");
                    return(BadRequest(ModelState));
                }
                var record    = _mapper.Map <Book>(createBookDTO);
                var isSuccess = await _bookRepository.Create(record);

                if (!isSuccess)
                {
                    _logger.LogWarn($"{ GetCallLocation()}:Create in db goes wrong");
                    return(StatusCode(500, "Internal Error"));
                }
                if (!string.IsNullOrEmpty(createBookDTO.File))
                {
                    var    imgPath    = $"{_env.ContentRootPath}\\uploads\\{createBookDTO.Image}";
                    byte[] imageBytes = Convert.FromBase64String(createBookDTO.File);
                    System.IO.File.WriteAllBytes(imgPath, imageBytes);
                }
                return(Created("Created", new { record }));
            }
            catch (Exception e)
            {
                _logger.LogInfo($"{ GetCallLocation()}:{e.Message} - {e.InnerException}");
                return(StatusCode(500, "Internal Error"));
            }
        }
 public void Put(int id, [FromBody] string value)
 {
     _logger.LogWarn("Accessed Home Put - Warning");
 }
Ejemplo n.º 7
0
 public void Delete(int id)
 {
     _logger.LogWarn("this is a warningr");
 }