Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlcf88 committed Jun 30, 2022
2 parents 4d963c8 + 5ac6df5 commit a0d4755
Show file tree
Hide file tree
Showing 37 changed files with 1,437 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public class BookingPeriodDto
public int TotalVolume { get; set; }

public int AvailableVolume { get; set; }

public TimeSpan Duration => EndingTime - StartingTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace EasyAbp.BookingService.AssetPeriodSchemes.Dtos;

[Serializable]
public class CreateUpdateAssetPeriodSchemeDto : ExtensibleObject
public class CreateAssetPeriodSchemeDto : ExtensibleObject
{
public Guid PeriodSchemeId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Volo.Abp.ObjectExtending;

namespace EasyAbp.BookingService.AssetPeriodSchemes.Dtos;

[Serializable]
public class UpdateAssetPeriodSchemeDto : ExtensibleObject
{
public DateTime Date { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public interface IAssetPeriodSchemeAppService :
AssetPeriodSchemeDto,
AssetPeriodSchemeKey,
GetAssetPeriodSchemesRequestDto,
CreateUpdateAssetPeriodSchemeDto,
CreateUpdateAssetPeriodSchemeDto>
CreateAssetPeriodSchemeDto,
UpdateAssetPeriodSchemeDto>
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
namespace EasyAbp.BookingService.AssetPeriodSchemes;

public class AssetPeriodSchemeAppService : AbstractKeyCrudAppService<AssetPeriodScheme, AssetPeriodSchemeDto,
AssetPeriodSchemeKey, GetAssetPeriodSchemesRequestDto, CreateUpdateAssetPeriodSchemeDto,
CreateUpdateAssetPeriodSchemeDto>,
AssetPeriodSchemeKey, GetAssetPeriodSchemesRequestDto, CreateAssetPeriodSchemeDto,
UpdateAssetPeriodSchemeDto>,
IAssetPeriodSchemeAppService
{
protected override string GetPolicyName { get; set; } = BookingServicePermissions.AssetPeriodScheme.Default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ public BookingServiceApplicationAutoMapperProfile()
CreateMap<PeriodScheme, PeriodSchemeDto>();
CreateMap<Period, PeriodDto>();
CreateMap<PeriodOccupancyModel, BookingPeriodDto>();

CreateMap<CreateAssetPeriodSchemeDto, AssetPeriodScheme>();
CreateMap<UpdateAssetPeriodSchemeDto, AssetPeriodScheme>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,25 @@ public virtual async Task<PeriodSchemeDto> DeletePeriodAsync(Guid periodSchemeId
{
var periodScheme = await GetEntityByIdAsync(periodSchemeId);

var period = periodScheme.Periods.Single(x => x.Id == periodId);

periodScheme.Periods.Remove(period);
await _periodSchemeManager.DeletePeriodAsync(periodScheme, periodId);

await _repository.UpdateAsync(periodScheme, true);

return await MapToGetOutputDtoAsync(periodScheme);
}

public override async Task DeleteAsync(Guid id)
{
await CheckDeletePolicyAsync();
var entity = await GetEntityByIdAsync(id);
if (await _periodSchemeManager.IsPeriodSchemeInUseAsync(entity))
{
throw new CannotDeletePeriodSchemeInUseException(entity.Id, entity.Name);
}

await _repository.DeleteAsync(entity, true);
}

protected override async Task<PeriodSchemeDto> MapToGetOutputDtoAsync(PeriodScheme entity)
{
var dto = await base.MapToGetOutputDtoAsync(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ public static class BookingServiceErrorCodes
public const string DisabledAssetOrCategory = "EasyAbp.BookingService:DisabledAssetOrCategory";
public const string DefaultPeriodSchemeNotFound = "EasyAbp.BookingService:DefaultPeriodSchemeNotFound";
public const string UnexpectedNegativeVolume = "EasyAbp.BookingService:UnexpectedNegativeVolume";
public const string CannotDeletePeriodInUse = "EasyAbp.BookingService:CannotDeletePeriodInUse";
public const string CannotUpdatePeriodInUse = "EasyAbp.BookingService:CannotUpdatePeriodInUse";
public const string CannotDeletePeriodSchemeInUse = "EasyAbp.BookingService:CannotDeletePeriodSchemeInUse";
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"EasyAbp.BookingService:InsufficientAssetVolume": "Occupancy failed due to insufficient asset volume.",
"EasyAbp.BookingService:DisabledAssetOrCategory": "Disabled asset or category.",
"EasyAbp.BookingService:DefaultPeriodSchemeNotFound": "The default period scheme is not found.",
"EasyAbp.BookingService:UnexpectedNegativeVolume": "AssetOccupancyCount(Asset:{assetId}, Date:{date}, StartingTime:{startingTime}, Duration:{duration})'s volume({volume},{changedVolume}) cannot less than zero. "
"EasyAbp.BookingService:UnexpectedNegativeVolume": "AssetOccupancyCount(Asset:{assetId}, Date:{date}, StartingTime:{startingTime}, Duration:{duration})'s volume({volume},{changedVolume}) cannot less than zero. ",
"EasyAbp.BookingService:CannotDeletePeriodInUse": "Can not delete Period which is in use. PeriodId: {id}",
"EasyAbp.BookingService:CannotUpdatePeriodInUse": "Can not update Period which is in use. PeriodId: {id}",
"EasyAbp.BookingService:CannotDeletePeriodSchemeInUse": "Can not delete PeriodScheme which is in use. PeriodScheme: {name}, Id: {id}. "
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"EasyAbp.BookingService:InsufficientAssetVolume": "由于资产剩余容量不足而占用失败",
"EasyAbp.BookingService:DisabledAssetOrCategory": "禁用的资产或资产目录",
"EasyAbp.BookingService:DefaultPeriodSchemeNotFound": "默认时段方案不存在",
"EasyAbp.BookingService:UnexpectedNegativeVolume": "资产占用计数(资产:{assetId}, 日期:{date}, 开始时间:{startingTime}, 持续时间:{duration})的volume({volume},{changedVolume})不能小于0"
"EasyAbp.BookingService:UnexpectedNegativeVolume": "资产占用计数(资产:{assetId}, 日期:{date}, 开始时间:{startingTime}, 持续时间:{duration})的volume({volume},{changedVolume})不能小于0",
"EasyAbp.BookingService:CannotDeletePeriodInUse": "不能删除正在使用的时段。时段Id:{id}",
"EasyAbp.BookingService:CannotUpdatePeriodInUse": "不能更新正在使用的时段。时段Id:{id}",
"EasyAbp.BookingService:CannotDeletePeriodSchemeInUse": "不能删除正在使用的时段方案:{name}, Id:{id}. "
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"EasyAbp.BookingService:InsufficientAssetVolume": "由於資產剩餘容量不足而佔用失敗",
"EasyAbp.BookingService:DisabledAssetOrCategory": "禁用的資產或資產目錄",
"EasyAbp.BookingService:DefaultPeriodSchemeNotFound": "默認時段不存在",
"EasyAbp.BookingService:UnexpectedNegativeVolume": "資產占用計數(資產:{assetId}, 日期:{date}, 開始時間:{startingTime}, 持續時間:{duration})的volume({volume},{changedVolume})不能小於0"
"EasyAbp.BookingService:UnexpectedNegativeVolume": "資產占用計數(資產:{assetId}, 日期:{date}, 開始時間:{startingTime}, 持續時間:{duration})的volume({volume},{changedVolume})不能小於0",
"EasyAbp.BookingService:CannotDeletePeriodInUse": "不能刪除正在使用的時段。時段Id:{id}",
"EasyAbp.BookingService:CannotUpdatePeriodInUse": "不能更新正在使用的時段。時段Id:{id}",
"EasyAbp.BookingService:CannotDeletePeriodSchemeInUse": "不能刪除正在使用的時段方案:{name}, Id:{id}. "
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Volo.Abp;

namespace EasyAbp.BookingService.PeriodSchemes;

public class CannotDeletePeriodInUseException : BusinessException
{
public CannotDeletePeriodInUseException(Guid id) : base(BookingServiceErrorCodes.CannotDeletePeriodInUse)
{
WithData(nameof(id), id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Volo.Abp;

namespace EasyAbp.BookingService.PeriodSchemes;

public class CannotDeletePeriodSchemeInUseException : BusinessException
{
public CannotDeletePeriodSchemeInUseException(Guid id, string name) : base(BookingServiceErrorCodes
.CannotDeletePeriodSchemeInUse)
{
WithData(nameof(id), id);
WithData(nameof(name), name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Volo.Abp;

namespace EasyAbp.BookingService.PeriodSchemes;

public class CannotUpdatePeriodInUseException : BusinessException
{
public CannotUpdatePeriodInUseException(Guid id) : base(BookingServiceErrorCodes.CannotUpdatePeriodInUse)
{
WithData(nameof(id), id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.BookingService.AssetCategories;
using EasyAbp.BookingService.AssetPeriodSchemes;
using EasyAbp.BookingService.Assets;
using EasyAbp.BookingService.AssetSchedules;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace EasyAbp.BookingService.PeriodSchemes;

public class PeriodSchemeManager : DomainService
{
protected IPeriodSchemeRepository Repository { get; }
protected IPeriodSchemeRepository Repository =>
LazyServiceProvider.LazyGetRequiredService<IPeriodSchemeRepository>();

public PeriodSchemeManager(IPeriodSchemeRepository repository)
{
Repository = repository;
}
protected IAssetCategoryRepository AssetCategoryRepository =>
LazyServiceProvider.LazyGetRequiredService<IAssetCategoryRepository>();

protected IAssetRepository AssetRepository =>
LazyServiceProvider.LazyGetRequiredService<IAssetRepository>();

protected IAssetPeriodSchemeRepository AssetPeriodSchemeRepository =>
LazyServiceProvider.LazyGetRequiredService<IAssetPeriodSchemeRepository>();

protected IAssetScheduleRepository AssetScheduleRepository =>
LazyServiceProvider.LazyGetRequiredService<IAssetScheduleRepository>();

public virtual Task<PeriodScheme> CreateAsync(string name, List<Period> periods)
{
Expand All @@ -29,14 +42,30 @@ public virtual Task<Period> CreatePeriodAsync(TimeSpan startingTime, TimeSpan du
return Task.FromResult(new Period(GuidGenerator.Create(), startingTime, duration));
}

public virtual Task<Period> UpdatePeriodAsync(PeriodScheme periodScheme, Guid periodId, TimeSpan startingTime,
public virtual async Task<Period> UpdatePeriodAsync(PeriodScheme periodScheme, Guid periodId, TimeSpan startingTime,
TimeSpan duration)
{
var period = periodScheme.Periods.Single(x => x.Id == periodId);
if (await IsPeriodInUseAsync(period))
{
throw new CannotUpdatePeriodInUseException(period.Id);
}

period.Update(startingTime, duration);

return Task.FromResult(period);
return period;
}

public virtual async Task<Period> DeletePeriodAsync(PeriodScheme periodScheme, Guid periodId)
{
var period = periodScheme.Periods.Single(x => x.Id == periodId);
if (await IsPeriodInUseAsync(period))
{
throw new CannotDeletePeriodInUseException(period.Id);
}

periodScheme.Periods.Remove(period);
return period;
}

public virtual Task UnsetDefaultAsync(PeriodScheme entity)
Expand Down Expand Up @@ -65,4 +94,34 @@ public virtual async Task SetAsDefaultAsync(PeriodScheme entity)

entity.UpdateIsDefault(true);
}

public virtual async Task<bool> IsPeriodSchemeInUseAsync(PeriodScheme periodScheme)
{
var assetCategory = await AssetCategoryRepository.FirstOrDefaultAsync(x => x.PeriodSchemeId == periodScheme.Id);
if (assetCategory is not null)
{
return true;
}

var asset = await AssetRepository.FirstOrDefaultAsync(x => x.PeriodSchemeId == periodScheme.Id);
if (asset is not null)
{
return true;
}

var assetSchedule = await AssetScheduleRepository.FirstOrDefaultAsync(x => x.PeriodSchemeId == periodScheme.Id);
if (assetSchedule is not null)
{
return true;
}

var assetPeriodScheme =
await AssetPeriodSchemeRepository.FirstOrDefaultAsync(x => x.PeriodSchemeId == periodScheme.Id);
return assetPeriodScheme is not null;
}

protected virtual Task<bool> IsPeriodInUseAsync(Period period)
{
return AssetScheduleRepository.AnyAsync(x => x.PeriodId == period.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public AssetPeriodSchemeController(IAssetPeriodSchemeAppService service)

[HttpPost]
[Route("")]
public virtual Task<AssetPeriodSchemeDto> CreateAsync(CreateUpdateAssetPeriodSchemeDto input)
public virtual Task<AssetPeriodSchemeDto> CreateAsync(CreateAssetPeriodSchemeDto input)
{
return _service.CreateAsync(input);
}

[HttpPut]
[Route("{AssetId}/{Date}")]
public virtual Task<AssetPeriodSchemeDto> UpdateAsync(AssetPeriodSchemeKey id, CreateUpdateAssetPeriodSchemeDto input)
public virtual Task<AssetPeriodSchemeDto> UpdateAsync(AssetPeriodSchemeKey id, UpdateAssetPeriodSchemeDto input)
{
return _service.UpdateAsync(id, input);
}
Expand Down
Loading

0 comments on commit a0d4755

Please sign in to comment.