using Api.SeaHavenIndustries.DTOs; using Api.SeaHavenIndustries.Helper; using Api.SeaHavenIndustries.Helper.ViewModel; using Data.SeaHavenIndustries; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using SeaHaven.Services.Interfaces; namespace Api.SeaHavenIndustries.Controllers { [Authorize] [ApiController] [Route("[controller]")] public class SettingsController : Controller { private readonly ISettingsService _settingsService; private readonly ILogger _logger; public SettingsController(ISettingsService settingsService, ILogger logger) { _settingsService = settingsService; _logger = logger; } [HttpPost] [Route("AddCategory")] public async Task AddCategory(Category_DTO model, CancellationToken cancellationToken) { try { await _settingsService.AddCategoryAsync(model.Name, cancellationToken); return Ok(new DataResponse { Message = "Successfully Saved Data", Status = "200" }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpPost] [Route("EditCategory")] public async Task EditCategory(EditCategory_DTO model, CancellationToken cancellationToken) { try { var found = await _settingsService.UpdateCategoryAsync(model.Id, model.Name, cancellationToken); if (!found) return BadRequest(new Response { Status = "Error", Message = "Category not found" }); return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpGet] [Route("DeleteCategory")] public async Task DeleteCategory(int id, CancellationToken cancellationToken) { try { var found = await _settingsService.DeleteCategoryAsync(id, cancellationToken); if (!found) return BadRequest(new Response { Status = "Error", Message = "ID not matched!" }); return Ok(new DataResponse { Message = "Deleted Successfully", Status = "200" }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpGet] [Route("GetCategoryList")] public async Task GetCategoryList(int page = 1, int pageSize = 10, CancellationToken cancellationToken = default) { var (items, total) = await _settingsService.GetCategoryListAsync(page, pageSize, cancellationToken); var totalPages = (int)Math.Ceiling(total / (double)pageSize); var viewModel = new Pagination_DTO { Data = items, PageNumber = page, PageSize = pageSize, TotalCount = total, TotalPages = totalPages }; return Ok(viewModel); } [HttpGet] [Route("GetTemplates")] public async Task GetTemplates(int page = 1, int pageSize = 10, CancellationToken cancellationToken = default) { try { var (items, total) = await _settingsService.GetTemplatesAsync(page, pageSize, cancellationToken); var totalPages = (int)Math.Ceiling(total / (double)pageSize); var viewModel = new Pagination_DTO { Data = items, PageNumber = page, PageSize = pageSize, TotalCount = total, TotalPages = totalPages }; return Ok(viewModel); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpGet] [Route("GetUsers")] public async Task GetUsers(string? search = "", int page = 1, int pageSize = 10, CancellationToken cancellationToken = default) { try { var (users, total) = await _settingsService.GetUsersAsync(search, page, pageSize, cancellationToken); var filteredUsers = users.Select(u => new UserRoleViewModel { RoleName = u.RoleName, Email = u.Email, UserName = u.UserName, Date = u.Date, Status = u.Status, Id = u.Id }).ToList(); var totalPages = (int)Math.Ceiling(total / (double)pageSize); var viewModel = new Pagination_DTO { Data = filteredUsers, PageNumber = page, PageSize = pageSize, TotalCount = total, TotalPages = totalPages }; return Ok(viewModel); } catch (Exception ex) { return BadRequest(new { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpGet] [Route("GetRoles")] public async Task GetRoles(CancellationToken cancellationToken) { try { var roles = await _settingsService.GetRolesAsync(cancellationToken); var result = roles.Select(r => new Roles_DTO { Name = r.Name, Description = r.Description }).ToList(); return Ok(result); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } } }