SHA256
GuestLog - StayEntry detail view and improvements:
- Added detail view for StayEntries. - Added button to delete StayEntries. - Improve queries of StayEntries using a "query config" record. - Enabled LazyLoading for all database relations. This will make easier to find cases where required relations aren't explicitly loaded.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Somnia.GuestLog.Models.DTOs;
|
||||
using Somnia.GuestLog.Models.QueryConfigs;
|
||||
|
||||
namespace Somnia.GuestLog.Services;
|
||||
|
||||
@@ -9,11 +10,11 @@ public interface IStayEntryService
|
||||
public Task<bool> DeleteEntryAsync(StayEntryDTO dto);
|
||||
public Task<bool> DeleteEntryAsync(long id);
|
||||
|
||||
public Task<StayEntryDTO?> GetDtoByIdAsync(long id);
|
||||
public Task<IEnumerable<StayEntryDTO>> GetDtoListAsync();
|
||||
public Task<StayEntryDTO?> GetDtoByIdAsync(long id, StayEntryQueryConfig? queryConfig = null);
|
||||
public Task<IEnumerable<StayEntryDTO>> GetDtoListAsync(StayEntryQueryConfig? queryConfig = null);
|
||||
|
||||
|
||||
public Task<bool> IsGuestInStayInPeriod(string docType, string docNum, DateTime startTime, DateTime endTime);
|
||||
public Task<IEnumerable<StayEntryDTO>> GetStaysWithGuest(string docType, string docNum);
|
||||
public Task<IEnumerable<StayEntryDTO>> GetStaysWithGuest(string docType, string docNum, StayEntryQueryConfig? queryConfig = null);
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Somnia.GuestLog.Infrastructure;
|
||||
using Somnia.GuestLog.Models;
|
||||
using Somnia.GuestLog.Models.DTOs;
|
||||
using Somnia.GuestLog.Models.Entities;
|
||||
using Somnia.GuestLog.Models.QueryConfigs;
|
||||
|
||||
namespace Somnia.GuestLog.Services;
|
||||
|
||||
@@ -41,7 +42,7 @@ public class StayEntryService(
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
|
||||
//Check and add GuestDocuments
|
||||
foreach (GuestDocumentDTO guestDto in dto.GuestDocuments)
|
||||
foreach (GuestDocumentDTO guestDto in dto.GuestDocuments!)
|
||||
{
|
||||
//Get it from db.
|
||||
GuestDocument? dbGuest = await dbContext.GuestDocuments.FindAsync(guestDto.DocumentType, guestDto.DocumentNum)
|
||||
@@ -111,21 +112,27 @@ public class StayEntryService(
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<StayEntryDTO?> GetDtoByIdAsync(long id)
|
||||
public async Task<StayEntryDTO?> GetDtoByIdAsync(long id, StayEntryQueryConfig? queryConfig = null)
|
||||
{
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
StayEntry? dbItem = await dbContext.StayEntries.FindAsync(id);
|
||||
return dbItem is not null ? GetDtoFromDb(dbItem) : null;
|
||||
StayEntry? dbItem = await dbContext.StayEntries
|
||||
.Where(e => e.Id == id)
|
||||
.AddQueryConfig(queryConfig)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return dbItem is not null ? GetDtoFromDb(dbItem, queryConfig) : null;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<StayEntryDTO>> GetDtoListAsync()
|
||||
public async Task<IEnumerable<StayEntryDTO>> GetDtoListAsync(StayEntryQueryConfig? queryConfig = null)
|
||||
{
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
|
||||
var dbList = await dbContext.StayEntries
|
||||
.AddQueryConfig(queryConfig)
|
||||
.OrderByDescending(e => e.UpdateTime)
|
||||
.ToListAsync();
|
||||
|
||||
return dbList.Select(GetDtoFromDb);
|
||||
return dbList.Select(e => GetDtoFromDb(e, queryConfig));
|
||||
}
|
||||
|
||||
|
||||
@@ -133,29 +140,33 @@ public class StayEntryService(
|
||||
{
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
|
||||
bool rs = await dbContext.StayEntries.AnyAsync(
|
||||
s => s.DateIn < endTime && s.DateOut > startTime
|
||||
&& s.GuestDocuments.Any(g => g.DocumentType == docType && g.DocumentNum == docNum));
|
||||
bool rs = await dbContext.StayEntries
|
||||
.AnyAsync(s => s.DateIn < endTime && s.DateOut > startTime
|
||||
&& s.GuestDocuments.Any(g => g.DocumentType == docType && g.DocumentNum == docNum)
|
||||
);
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<StayEntryDTO>> GetStaysWithGuest(string docType, string docNum)
|
||||
public async Task<IEnumerable<StayEntryDTO>> GetStaysWithGuest(string docType, string docNum, StayEntryQueryConfig? queryConfig = null)
|
||||
{
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
|
||||
var rs = await dbContext.StayEntries.Where(
|
||||
s => s.GuestDocuments.Any(g => g.DocumentType == docType && g.DocumentNum == docNum))
|
||||
var rs = await dbContext.StayEntries
|
||||
.Where(s => s.GuestDocuments.Any(g => g.DocumentType == docType && g.DocumentNum == docNum))
|
||||
.AddQueryConfig(queryConfig)
|
||||
.ToListAsync();
|
||||
|
||||
return rs.Select(GetDtoFromDb);
|
||||
return rs.Select(e => GetDtoFromDb(e, queryConfig));
|
||||
}
|
||||
|
||||
#region Internal Methods
|
||||
|
||||
private static StayEntryDTO GetDtoFromDb(StayEntry dbItem)
|
||||
private static StayEntryDTO GetDtoFromDb(StayEntry dbItem, StayEntryQueryConfig? queryConfig)
|
||||
{
|
||||
return EntityMapping.MapStayEntryEntityToDTO(dbItem);
|
||||
queryConfig ??= StayEntryQueryConfig.DefaultQueryConfig;
|
||||
|
||||
return EntityMapping.MapStayEntryEntityToDTO(dbItem, queryConfig.IncludeGuests);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user