shoc-backend/SeaHavenIndustries/Data/Services/ContactService.cs
Adam Moussa dbb31ca7d7 Fix Contacts.Name references throughout Blazor project
- Replace all Contacts.Name with (FirstName + " " + LastName) pattern
- Fix ContactService, Index.razor, QuotesPDF, QuotesDetails, CreateQuotes
- Fix WorkOrderPDF, Viewworkorder, UserViewWordOrder, Addworkorders
- Fix ViewTemplate, AddTemplate, Templates settings pages
- Blazor project now builds successfully
2026-04-16 16:54:28 -04:00

80 lines
2.4 KiB
C#

using Data.SeaHavenIndustries;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.JSInterop;
using SeaHavenIndustries.Data.IRepository;
using System.Text.Json;
namespace SeaHavenIndustries.Data.Services
{
public class ContactService : IContactRepository
{
private readonly ApplicationDbContext _context;
public ContactService(ApplicationDbContext context)
{
_context = context;
}
public async Task<string> AddContact(Contacts contacts)
{
_context.ChangeTracker.Clear();
try
{
var exist = _context.Contacts.Where(w => w.Id == contacts.Id).FirstOrDefault();
if (exist != null)
{
exist.FirstName = contacts.FirstName;
exist.LastName = contacts.LastName;
exist.PhoneNumber = contacts.PhoneNumber;
exist.Email = contacts.Email;
_context.Update(exist);
}
else
{
_context.Contacts.Add(contacts);
}
await _context.SaveChangesAsync();
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
public async Task<List<Contacts>> GetContacts()
{
var list = await _context.Contacts.ToListAsync();
return list;
}
public async Task<IQueryable<Contacts>> GetContactsiqueryable()
{
var list = _context.Contacts;
return list;
}
public async Task<Contacts> GetContactById(int id)
{
var data = await _context.Contacts.Where(w => w.Id == id).FirstOrDefaultAsync();
return data ?? new Contacts();
}
public async Task<string> DeleteContact(int id)
{
_context.ChangeTracker.Clear();
try
{
var data = await _context.Contacts.Where(w => w.Id == id).FirstOrDefaultAsync();
if (data != null)
{
_context.Contacts.Remove(data);
}
await _context.SaveChangesAsync();
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}