コード例 #1
0
        public async Task <IActionResult> InventoryDelete(int id)
        {
            string dealerId  = User.Claims.FirstOrDefault(x => x.Type == "DealerId").Value;
            var    imagePath = Path.Combine(
                _environment.WebRootPath,
                "images\\dealer\\" + dealerId.ToString() + "\\inventory\\" + dealerId
                );

            try
            {
                _outdoorPowerRepository.DeleteDealerInventory(id);

                if (await _outdoorPowerRepository.SaveChangesAsync())
                {
                    // Remove all images
                    if (Directory.Exists(imagePath))
                    {
                        DirectoryInfo d = new DirectoryInfo(imagePath);

                        foreach (FileInfo file in d.GetFiles())
                        {
                            file.Delete();
                        }
                    }

                    return(Ok(id));
                }
                else
                {
                    _logger.LogError($"Unable to save changes to database for inventory: {id}");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"An error occurred when deleting inventory (id: {id},  Error Message: {ex}");
            }

            return(BadRequest("Failed to delete the inventory."));
        }