shoc-backend/Api.SeaHavenIndustries/Controllers/LocationController.cs
Adam Moussa 93fa76a5aa Handle string-to-int AccountId conversion in Location DTOs
- Frontend sends accountId as empty string when not set
- Change DTO AccountId to string with GetAccountId() parser
- Prevents JSON deserialization error on empty/non-numeric values
2026-04-16 17:15:28 -04:00

185 lines
6.1 KiB
C#

using Api.SeaHavenIndustries.DTOs;
using Data.SeaHavenIndustries;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Api.SeaHavenIndustries.Controllers
{
[Authorize]
[ApiController]
[Route("api/[controller]")]
[Route("api/locations")]
public class LocationController : Controller
{
private readonly ApplicationDbContext _db;
public LocationController(ApplicationDbContext db)
{
_db = db;
}
[HttpGet("GetLocationList")]
public IActionResult GetLocationList(string? search = "", int page = 1, int pageSize = 10)
{
var query = _db.Locations.AsQueryable();
if (!string.IsNullOrWhiteSpace(search))
{
var s = search.ToLower();
query = query.Where(l =>
(l.Name ?? "").ToLower().Contains(s) ||
(l.Title ?? "").ToLower().Contains(s) ||
(l.Address ?? "").ToLower().Contains(s) ||
(l.City ?? "").ToLower().Contains(s));
}
var totalCount = query.Count();
var data = query
.OrderByDescending(l => l.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Select(l => new
{
l.Id,
l.Name,
l.Title,
l.Address,
l.City,
l.State,
l.ZipCode,
l.Phone,
l.Contact,
l.ContactEmail,
l.Status,
l.AccountId
})
.ToList();
var viewModel = new Pagination_DTO
{
Data = data,
PageNumber = page,
PageSize = pageSize,
TotalCount = totalCount,
TotalPages = (int)Math.Ceiling(totalCount / (double)pageSize)
};
return Ok(viewModel);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetLocationById(int id)
{
var location = await _db.Locations
.Where(l => l.Id == id)
.Select(l => new
{
l.Id,
l.Name,
l.Title,
l.Address,
l.City,
l.State,
l.ZipCode,
l.Phone,
l.Contact,
l.ContactEmail,
l.Status,
l.AccountId
})
.FirstOrDefaultAsync();
if (location == null)
return NotFound(new Response { Status = "Error", Message = "Location not found" });
return Ok(location);
}
[HttpPost]
public async Task<IActionResult> AddLocation([FromBody] Location_DTO model)
{
try
{
var location = new Locations
{
Name = model.Name,
Title = model.Title,
Address = model.Address,
City = model.City,
State = model.State,
ZipCode = model.ZipCode,
Phone = model.Phone,
Contact = model.Contact,
ContactEmail = model.ContactEmail,
Status = model.Status,
AccountId = model.GetAccountId()
};
_db.Locations.Add(location);
await _db.SaveChangesAsync();
return Ok(new DataResponse { Message = "Location Created Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = ex.Message });
}
}
[HttpPut("{id}")]
public async Task<IActionResult> EditLocation(int id, [FromBody] EditLocation_DTO model)
{
try
{
var exist = await _db.Locations.Where(l => l.Id == id).FirstOrDefaultAsync();
if (exist == null)
return BadRequest(new Response { Status = "Error", Message = "Location not found" });
exist.Name = model.Name;
exist.Title = model.Title;
exist.Address = model.Address;
exist.City = model.City;
exist.State = model.State;
exist.ZipCode = model.ZipCode;
exist.Phone = model.Phone;
exist.Contact = model.Contact;
exist.ContactEmail = model.ContactEmail;
exist.Status = model.Status;
exist.AccountId = model.GetAccountId();
await _db.SaveChangesAsync();
return Ok(new DataResponse { Message = "Location Updated Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = ex.Message });
}
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteLocation(int id)
{
try
{
var exist = await _db.Locations.Where(l => l.Id == id).FirstOrDefaultAsync();
if (exist == null)
return BadRequest(new Response { Status = "Error", Message = "Location not found" });
_db.Locations.Remove(exist);
await _db.SaveChangesAsync();
return Ok(new DataResponse { Message = "Location Deleted Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = ex.Message });
}
}
[HttpGet("DeleteLocation")]
public async Task<IActionResult> DeleteLocationByQuery(int id)
{
return await DeleteLocation(id);
}
}
}