Add new API

Update Salary
Swap Sequence
This commit is contained in:
Suphonchai Phoonsawat 2023-08-04 14:30:10 +07:00
parent 680bfba81e
commit a4e8b50d45
3 changed files with 196 additions and 0 deletions

View file

@ -1,4 +1,5 @@
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Requests.Commands;
using BMA.EHR.Domain.Models.Commands.Core;
using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Domain.Models.Organizations;
@ -7,6 +8,7 @@ using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Transactions;
using Command = BMA.EHR.Domain.Models.Commands.Core.Command;
namespace BMA.EHR.Application.Repositories.Commands
@ -76,6 +78,8 @@ namespace BMA.EHR.Application.Repositories.Commands
#endregion
#region " Command Receiver "
public async Task<List<CommandReceiver>> GetReceiverByCommmandIdAsync(Guid Id)
{
try
@ -167,6 +171,89 @@ namespace BMA.EHR.Application.Repositories.Commands
}
}
public async Task<CommandReceiver?> GetCommandReceiverAsync(Guid personalId)
{
try
{
var receiver = await _dbContext.Set<CommandReceiver>()
.FirstOrDefaultAsync(x => x.Id == personalId);
if (receiver == null)
throw new Exception(GlobalMessages.DataNotFound);
return receiver;
}
catch
{
throw;
}
}
public async Task SwapReceiverOrderAsync(Guid personalId, string direction)
{
try
{
var current = await _dbContext.Set<CommandReceiver>()
.FirstOrDefaultAsync(x => x.Id == personalId);
if (current == null)
throw new Exception(GlobalMessages.DataNotFound);
var currentSeq = current.Sequence;
switch (direction.Trim().ToLower())
{
case "up":
{
// get prev record
var prev = await _dbContext.Set<CommandReceiver>()
.OrderByDescending(x => x.Sequence)
.Where(x => x.Sequence < currentSeq)
.Take(1)
.FirstOrDefaultAsync();
if (prev != null)
{
var prevSeq = prev.Sequence;
current.Sequence = prevSeq;
prev.Sequence = currentSeq;
await _dbContext.SaveChangesAsync();
}
break;
}
case "down":
{
// get next record
var next = await _dbContext.Set<CommandReceiver>()
.OrderBy(x => x.Sequence)
.Where(x => x.Sequence > currentSeq)
.Take(1)
.FirstOrDefaultAsync();
if (next != null)
{
var nextSeq = next.Sequence;
current.Sequence = nextSeq;
next.Sequence = currentSeq;
await _dbContext.SaveChangesAsync();
}
break;
}
default: throw new Exception("Invalid swap direction!");
}
}
catch
{
throw;
}
}
#endregion
public async Task<List<CommandDeployment>> GetDeploymentByCommandIdAsync(Guid id)
{
try
@ -473,6 +560,28 @@ namespace BMA.EHR.Application.Repositories.Commands
#region " Placement "
public async Task UpdatePlacementSalaryAsync(Guid placementProfileId, UpdatePlacementSalaryRequest req)
{
try
{
var placementProfile = await _dbContext.Set<PlacementProfile>()
.FirstOrDefaultAsync(p => p.Id == placementProfileId);
if (placementProfile == null)
throw new Exception($"Invalid placement profile: {placementProfileId}");
placementProfile.Amount = req.SalaryAmount;
placementProfile.PositionSalaryAmount = req.PositionSalaryAmount;
placementProfile.MouthSalaryAmount = req.MonthSalaryAmount;
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task<List<PositionPath>> GetPlacementPositionPath(Guid id)
{
try