using Data.SeaHavenIndustries; using SeaHaven.DataServices.Interfaces; using SeaHaven.Services.DTOs; using SeaHaven.Services.Interfaces; namespace SeaHaven.Services.Implementation { public class CalendarService : ICalendarService { private readonly ICalendarDataService _dataService; public CalendarService(ICalendarDataService dataService) { _dataService = dataService; } public async Task CreateEventAsync(EditEventInput input, CancellationToken cancellationToken) { var model = new Events { Title = input.Title, Description = input.Description, Location = input.Location, RecordID = input.RecordID, RecordName = input.RecordName, RecordTargetType = input.RecordTargetType, ReminderMinutes = input.ReminderMinutes, EventColor = input.EventColor, StartDate = input.StartDate, StartTime = input.StartTime, EndDate = input.EndDate, EndTime = input.EndTime, AllDay = input.AllDay, CreatedDate = DateTime.Now, IsDeleted = false }; await _dataService.AddEventAsync(model, cancellationToken); } public async Task UpdateEventAsync(EditEventInput input, CancellationToken cancellationToken) { var model = await _dataService.GetEventForEditAsync(input.Id!.Value, cancellationToken); if (model == null || model.IsDeleted == true) return false; model.Title = input.Title; model.Description = input.Description; model.Location = input.Location; model.RecordID = input.RecordID; model.RecordName = input.RecordName; model.RecordTargetType = input.RecordTargetType; model.ReminderMinutes = input.ReminderMinutes; model.EventColor = input.EventColor; model.StartDate = input.StartDate; model.StartTime = input.StartTime; model.EndDate = input.EndDate; model.EndTime = input.EndTime; model.AllDay = input.AllDay; model.LastModificationTime = DateTime.Now; await _dataService.UpdateEventAsync(model, cancellationToken); return true; } public async Task GetEventByIdAsync(int id, CancellationToken cancellationToken) { var model = await _dataService.GetEventByIdAsync(id, cancellationToken); return model == null ? null : MapToDTO(model); } public async Task DeleteEventAsync(int id, CancellationToken cancellationToken) { var model = await _dataService.GetEventForEditAsync(id, cancellationToken); if (model == null) return false; model.IsDeleted = true; model.DeletionTime = DateTime.Now; await _dataService.UpdateEventAsync(model, cancellationToken); return true; } public async Task GetEventListAsync(string? search, int page, int pageSize, CancellationToken cancellationToken) { var (items, totalCount) = await _dataService.GetEventListPagedAsync(page, pageSize, search, cancellationToken); return new PagedEventsResult { Rows = items.Select(MapToDTO).ToList(), TotalCount = totalCount }; } public async Task> GetEventsForCalendarAsync(DateTime? start, DateTime? end, CancellationToken cancellationToken) { var events = await _dataService.GetEventsForCalendarAsync(start, end, cancellationToken); return events.Select(MapToDTO); } private static EventDTO MapToDTO(Events model) { return new EventDTO { Id = model.Id, Title = model.Title, Description = model.Description, Location = model.Location, RecordID = model.RecordID, RecordName = model.RecordName, RecordTargetType = model.RecordTargetType, ReminderMinutes = model.ReminderMinutes, EventColor = model.EventColor, StartDate = model.StartDate, StartTime = model.StartTime, EndDate = model.EndDate, EndTime = model.EndTime, AllDay = model.AllDay, CreatedDate = model.CreatedDate, createdby = model.createdby }; } } }