Files
constructdemo/ConstructorAppUI/Controllers/TestimonialController.cs
2025-05-01 15:18:30 +03:00

211 lines
8.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using ConstructorApp.EntityLayer.Entities;
using ConstructorAppUI.Dtos.TestimonialDtos;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Text;
namespace ConstructorAppUI.Controllers
{
public class TestimonialController : Controller
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly string _apiBaseUrl;
public TestimonialController(IHttpClientFactory httpClientFactory, IWebHostEnvironment webHostEnvironment, IConfiguration configuration)
{
_httpClientFactory = httpClientFactory;
_webHostEnvironment = webHostEnvironment;
_apiBaseUrl = configuration["ApiSettings:BaseUrl"];
}
public async Task<IActionResult> Index()
{
var client = _httpClientFactory.CreateClient();
var responseMessage = await client.GetAsync($"{_apiBaseUrl}/api/Testimonial");
if (responseMessage.IsSuccessStatusCode)
{
var jsonData = await responseMessage.Content.ReadAsStringAsync();
var values = JsonConvert.DeserializeObject<List<ResultTestimonialDto>>(jsonData);
// Pending olan öğeleri en üste getirmek için sıralama yapılıyor
var sortedValues = values.OrderByDescending(t => t.Status == TestimonialStatus.Pending).ToList();
return View(sortedValues);
}
return View();
}
[HttpGet]
public IActionResult CreateTestimonial()
{
return View();
}
[HttpPost]
public async Task<IActionResult> CreateTestimonial(CreateTestimonialDto createTestimonialDto, IFormFile ImageFile)
{
if (ImageFile != null)
{
var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Images");
// Klasör mevcut değilse oluştur
if (!Directory.Exists(imagePath))
{
Directory.CreateDirectory(imagePath);
}
var filePath = Path.Combine(imagePath, ImageFile.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await ImageFile.CopyToAsync(stream);
}
createTestimonialDto.ImageUrl = "/Images/" + ImageFile.FileName;
}
var client = _httpClientFactory.CreateClient();
var jsonData = JsonConvert.SerializeObject(createTestimonialDto);
StringContent stringContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
var responseMessage = await client.PostAsync($"{_apiBaseUrl}/api/Testimonial", stringContent);
if (responseMessage.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
return View();
}
[HttpGet]
public async Task<IActionResult> UpdateTestimonial(int id)
{
var client = _httpClientFactory.CreateClient();
var responseMessage = await client.GetAsync($"{_apiBaseUrl}/api/Testimonial/{id}");
if (responseMessage.IsSuccessStatusCode)
{
var jsonData = await responseMessage.Content.ReadAsStringAsync();
var values = JsonConvert.DeserializeObject<UpdateTestimonialDto>(jsonData);
return View(values);
}
return View();
}
[HttpPost]
public async Task<IActionResult> UpdateTestimonial(UpdateTestimonialDto updateTestimonialDto, IFormFile ImageFile)
{
var client = _httpClientFactory.CreateClient();
// Mevcut "Testimonial" verisini al
var existingSliderResponse = await client.GetAsync($"{_apiBaseUrl}/api/Testimonial/{updateTestimonialDto.TestimonialID}");
if (existingSliderResponse.IsSuccessStatusCode)
{
var existingJsonData = await existingSliderResponse.Content.ReadAsStringAsync();
var existingSlider = JsonConvert.DeserializeObject<UpdateTestimonialDto>(existingJsonData);
// Görseller için "wwwroot/Images" klasörünün varlığını kontrol et
var imagesPath = Path.Combine(_webHostEnvironment.WebRootPath, "Images");
if (!Directory.Exists(imagesPath))
{
Directory.CreateDirectory(imagesPath);
}
// ImageUrl için işlem
if (ImageFile != null && ImageFile.Length > 0)
{
// Eski görseli sil
if (!string.IsNullOrEmpty(existingSlider.ImageUrl))
{
var oldImagePath = Path.Combine(_webHostEnvironment.WebRootPath, existingSlider.ImageUrl.TrimStart('/'));
if (System.IO.File.Exists(oldImagePath))
{
System.IO.File.Delete(oldImagePath);
}
}
// Yeni görseli yükle
var fileName = Path.GetFileName(ImageFile.FileName);
var imagePath = Path.Combine(imagesPath, fileName);
using (var stream = new FileStream(imagePath, FileMode.Create))
{
await ImageFile.CopyToAsync(stream);
}
updateTestimonialDto.ImageUrl = $"/Images/{fileName}";
}
else
{
updateTestimonialDto.ImageUrl = existingSlider.ImageUrl;
}
// API'ye güncelleme isteği gönder
var updatedJsonData = JsonConvert.SerializeObject(updateTestimonialDto);
StringContent stringContent = new StringContent(updatedJsonData, Encoding.UTF8, "application/json");
var responseMessage = await client.PutAsync($"{_apiBaseUrl}/api/Testimonial/", stringContent);
if (responseMessage.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
}
return View(updateTestimonialDto);
}
[HttpGet]
public async Task<IActionResult> DeleteTestimonialConfirmation(int id)
{
var client = _httpClientFactory.CreateClient();
var responseMessage = await client.GetAsync($"{_apiBaseUrl}/api/Testimonial/{id}");
if (responseMessage.IsSuccessStatusCode)
{
var jsonData = await responseMessage.Content.ReadAsStringAsync();
var slider = JsonConvert.DeserializeObject<UpdateTestimonialDto>(jsonData);
return View(slider);
}
return View("Error");
}
[HttpPost]
public async Task<IActionResult> DeleteTestimonial(int id)
{
var client = _httpClientFactory.CreateClient();
var responseMessage = await client.GetAsync($"{_apiBaseUrl}/api/Testimonial/{id}");
if (responseMessage.IsSuccessStatusCode)
{
var jsonData = await responseMessage.Content.ReadAsStringAsync();
var slider = JsonConvert.DeserializeObject<UpdateTestimonialDto>(jsonData);
if (!string.IsNullOrEmpty(slider.ImageUrl))
{
var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Images", Path.GetFileName(slider.ImageUrl));
// Görselin gerçekten var olup olmadığını kontrol et
if (System.IO.File.Exists(imagePath))
{
System.IO.File.Delete(imagePath); // Sadece görsel dosyasını sil
}
}
var deleteResponse = await client.DeleteAsync($"{_apiBaseUrl}/api/Testimonial/{id}");
if (deleteResponse.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
}
return View("Error");
}
public async Task<IActionResult> TestimonialStatusActive(int id)
{
var client = _httpClientFactory.CreateClient();
await client.GetAsync($"{_apiBaseUrl}/api/Testimonial/TestimonialStatusActive/{id}");
return RedirectToAction("Index");
}
public async Task<IActionResult> TestimonialStatusPassive(int id)
{
var client = _httpClientFactory.CreateClient();
await client.GetAsync($"{_apiBaseUrl}/api/Testimonial/TestimonialStatusPassive/{id}");
return RedirectToAction("Index");
}
}
}