mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 14:13:12 +00:00
- services/api.js uses lowercase routes (/api/workorders, /api/employees, etc.)
- Add matching [Route] attributes to WorkOrder, Employee, Contact, Account, FollowUp, PMSchedule controllers
- Add [HttpGet("{id:int}")] to WorkOrderController for REST-style get by ID
206 lines
6.1 KiB
C#
206 lines
6.1 KiB
C#
using Api.SeaHavenIndustries.DTOs;
|
|
using Data.SeaHavenIndustries;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
[ApiController]
|
|
[Route("api/Account")]
|
|
[Route("api/accounts")]
|
|
public class AccountController : ControllerBase
|
|
{
|
|
private readonly ApplicationDbContext _db;
|
|
|
|
public AccountController(ApplicationDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
// -----------------------------------
|
|
// POST: api/Account/AddAccounts
|
|
// -----------------------------------
|
|
[HttpPost("AddAccounts")]
|
|
public async Task<IActionResult> AddAccounts(Account_DTO dto)
|
|
{
|
|
try
|
|
{
|
|
var model = new Accounts
|
|
{
|
|
Name = dto.Name,
|
|
Type = dto.Type,
|
|
Importance = dto.Importance,
|
|
TerritoryId = dto.TerritoryId,
|
|
OwnerEmployeeId = dto.OwnerEmployeeId,
|
|
PrivacySetting = dto.PrivacySetting,
|
|
About = dto.About,
|
|
Tags = dto.Tags,
|
|
|
|
Phone = dto.Phone,
|
|
Email = dto.Email,
|
|
IndustryId = dto.IndustryId,
|
|
TimeZone = dto.TimeZone,
|
|
Website = dto.Website,
|
|
CurrencyCode = dto.CurrencyCode,
|
|
ServiceInstructions = dto.ServiceInstructions,
|
|
|
|
FacebookUrl = dto.FacebookUrl,
|
|
LinkedInUrl = dto.LinkedInUrl,
|
|
TwitterUrl = dto.TwitterUrl,
|
|
|
|
CreatedDate = DateTime.UtcNow
|
|
};
|
|
|
|
_db.Accounts.Add(model);
|
|
await _db.SaveChangesAsync();
|
|
|
|
return Ok(new DataResponse
|
|
{
|
|
Message = "Account added successfully.",
|
|
Status = "200"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response
|
|
{
|
|
Status = "Error",
|
|
Message = ex.Message
|
|
});
|
|
}
|
|
}
|
|
|
|
// -----------------------------------
|
|
// POST: api/Account/EditAccounts
|
|
// -----------------------------------
|
|
[HttpPost("EditAccounts")]
|
|
public async Task<IActionResult> EditAccounts(EditAccount_DTO dto)
|
|
{
|
|
try
|
|
{
|
|
var account = await _db.Accounts.FirstOrDefaultAsync(a => a.Id == dto.Id);
|
|
if (account == null)
|
|
return BadRequest(new Response { Status = "Error", Message = "Account not found." });
|
|
|
|
account.Name = dto.Name;
|
|
account.Type = dto.Type;
|
|
account.Importance = dto.Importance;
|
|
account.TerritoryId = dto.TerritoryId;
|
|
account.OwnerEmployeeId = dto.OwnerEmployeeId;
|
|
account.PrivacySetting = dto.PrivacySetting;
|
|
account.About = dto.About;
|
|
account.Tags = dto.Tags;
|
|
|
|
account.Phone = dto.Phone;
|
|
account.Email = dto.Email;
|
|
account.IndustryId = dto.IndustryId;
|
|
account.TimeZone = dto.TimeZone;
|
|
account.Website = dto.Website;
|
|
account.CurrencyCode = dto.CurrencyCode;
|
|
account.ServiceInstructions = dto.ServiceInstructions;
|
|
|
|
account.FacebookUrl = dto.FacebookUrl;
|
|
account.LinkedInUrl = dto.LinkedInUrl;
|
|
account.TwitterUrl = dto.TwitterUrl;
|
|
|
|
account.LastModificationTime = DateTime.UtcNow;
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
return Ok(new DataResponse
|
|
{
|
|
Message = "Account updated successfully.",
|
|
Status = "200"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response
|
|
{
|
|
Status = "Error",
|
|
Message = ex.Message
|
|
});
|
|
}
|
|
}
|
|
|
|
// -----------------------------------
|
|
// GET: api/Account/DeleteAccounts?id=5
|
|
// -----------------------------------
|
|
[HttpGet("DeleteAccounts")]
|
|
public async Task<IActionResult> DeleteAccounts(int id)
|
|
{
|
|
try
|
|
{
|
|
var account = await _db.Accounts.FirstOrDefaultAsync(a => a.Id == id);
|
|
if (account == null)
|
|
return BadRequest(new Response { Status = "Error", Message = "Account not found." });
|
|
|
|
_db.Accounts.Remove(account);
|
|
await _db.SaveChangesAsync();
|
|
|
|
return Ok(new DataResponse
|
|
{
|
|
Message = "Account deleted successfully.",
|
|
Status = "200"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response
|
|
{
|
|
Status = "Error",
|
|
Message = ex.Message
|
|
});
|
|
}
|
|
}
|
|
|
|
// -----------------------------------
|
|
// GET: api/Account/GetAccountList?search=&page=&pageSize=
|
|
// -----------------------------------
|
|
[HttpGet("GetAccountList")]
|
|
public IActionResult GetAccountList(string? search = "", int page = 1, int pageSize = 12)
|
|
{
|
|
search ??= "";
|
|
|
|
var query = _db.Accounts
|
|
.Where(a => a.IsDeleted == false || a.IsDeleted == null);
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
var s = search.ToLower();
|
|
query = query.Where(a =>
|
|
(a.Name ?? "").ToLower().Contains(s) ||
|
|
(a.Email ?? "").ToLower().Contains(s) ||
|
|
(a.Phone ?? "").Contains(s) ||
|
|
(a.Website ?? "").ToLower().Contains(s)
|
|
);
|
|
}
|
|
|
|
var totalCount = query.Count();
|
|
|
|
var data = query
|
|
.OrderByDescending(a => a.Id)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.Select(a => new
|
|
{
|
|
a.Id,
|
|
a.Name,
|
|
a.Type,
|
|
a.Phone,
|
|
a.Email,
|
|
Industry = a.IndustryId,
|
|
a.Website
|
|
})
|
|
.ToList();
|
|
|
|
var viewModel = new Pagination_DTO
|
|
{
|
|
Data = data,
|
|
PageNumber = page,
|
|
PageSize = pageSize,
|
|
TotalCount = totalCount,
|
|
TotalPages = (int)Math.Ceiling(totalCount / (double)pageSize)
|
|
};
|
|
|
|
return Ok(viewModel);
|
|
}
|
|
}
|