Esempio n. 1
0
        public async Task <string> DeletePostAsync([FromBody] PortfolioRequestModel body)
        {
            if (!PortfolioPostModelExists(body.Id))
            {
                return("Invalid post ID.");
            }

            try
            {
                PortfolioPostModel post = await _context.PortfolioPosts.FindAsync(body.Id);

                var existingFilePath = _publicDir + post.ImageSource;
                if (System.IO.File.Exists(existingFilePath))
                {
                    System.IO.File.Delete(existingFilePath);
                }

                _context.PortfolioPosts.Remove(post);
                await _context.SaveChangesAsync();

                return("Post successfully deleted!");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{nameof(DeletePostAsync)} => FAIL with args: {body}");
            }

            return("Something went wrong. Try again!");
        }
Esempio n. 2
0
        public async Task <PortfolioPostModel> ReadPostAsync([FromBody] PortfolioRequestModel body)
        {
            try
            {
                if (!PortfolioPostModelExists(body.Id))
                {
                    return(new PortfolioPostModel());
                }

                return(await _context.PortfolioPosts.FindAsync(body.Id));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{nameof(ReadPostAsync)} => FAIL with args: {body}");
            }

            return(new PortfolioPostModel());
        }