#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using LinqKit.Core;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.EventBus.Distributed;
using rupbes.firstapp.Phbk;
using rupbes.firstapp.Permissions;
/*
// == 1 ==
// modify "firstappPermissions"-class like seen
public class firstappPermissions
{
...
public static class PhbkDivisionDto
{
public const string Default = GroupName + ".PhbkDivisionDto";
public const string FullScan = Default + ".f";
public const string Create = Default + ".a";
public const string Edit = Default + ".u";
public const string Delete = Default + ".d";
}
...
}
// == 2 ==
// modify "firstappPermissionDefinitionProvider"-class like seen
public class firstappPermissionDefinitionProvider : PermissionDefinitionProvider
{
...
public override void Define(IPermissionDefinitionContext context)
{
var mdlGrp = context.AddGroup(firstappPermissions.GroupName, L("Psn:firstapp"));
var perm = mdlGrp.AddPermission(firstappPermissions.PhbkDivisionDto.Default, L("Psn:PhbkDivisionDto"));
perm.AddChild(firstappPermissions.PhbkDivisionDto.FullScan, L("Psn:PhbkDivisionDto.f"));
perm.AddChild(firstappPermissions.PhbkDivisionDto.Create, L("Psn:PhbkDivisionDto.a"));
perm.AddChild(firstappPermissions.PhbkDivisionDto.Edit, L("Psn:PhbkDivisionDto.u"));
perm.AddChild(firstappPermissions.PhbkDivisionDto.Delete, L("Psn:PhbkDivisionDto.d"));
}
...
}
*/
namespace rupbes.firstapp.PhBk {
//
// Please replace ApplicationService with a name of the ApplicationService of your project
//
[Authorize(firstappPermissions.PhbkDivisionDto.Default)]
public class PhbkDivisionDtoService: ApplicationService, IPhbkDivisionDtoService
{
private int defaultPageSize = 50;
private int minPageSize = 5;
private int maxPageSize = 150;
// private IRepository<PhbkDivision, System.Int32> repo;
// public PhbkDivisionDtoService(IRepository<PhbkDivision, System.Int32> _repo) { repo = _repo; }
private IPhbkDivisionDtoRepo repo;
private readonly IDistributedEventBus _distributedEventBus;
public PhbkDivisionDtoService(IPhbkDivisionDtoRepo _repo, IDistributedEventBus distributedEventBus )
{
repo = _repo;
_distributedEventBus = distributedEventBus;
}
[Authorize(firstappPermissions.PhbkDivisionDto.FullScan)]
public async Task<IEnumerable<PhbkDivisionDto>> getall()
{
// using (repo.DisableTracking())
{
var appQr = (await repo.GetQueryableAsync()) // .PhbkDivisionDbSet
.Select(itm => new PhbkDivisionDto() {
Id = itm.Id
, DivisionName = itm.DivisionName
, DivisionDesc = itm.DivisionDesc
, ConcurrencyStamp = itm.ConcurrencyStamp
, EntrprsIdRef = itm.EntrprsIdRef
});
return await AsyncExecuter.ToListAsync(appQr);
} // using (repo.DisableTracking()) { ... }
} // the end of Get()-method
[Authorize(firstappPermissions.PhbkDivisionDto.FullScan)]
public async Task<PhbkDivisionDtoPage> getwithfilter(
System.Int32?[] id,
string[] idOprtr,
System.String[] divisionName,
string[] divisionNameOprtr,
System.Int32?[] entrprsIdRef,
string[] entrprsIdRefOprtr,
string[] orderby = null,
int? page =null,
int? pagesize = null)
{
string[] EqualOperators = { "eq", "lk" };
string[] ExpectedOperators = { "eq", "lk", "gt", "lt", "ne", "cn", "sw" };
int currentPageSize = this.defaultPageSize;
int currentPage = 1;
if (pagesize.HasValue) {
currentPageSize = pagesize.Value;
if ((currentPageSize < this.minPageSize) || (currentPageSize > this.maxPageSize)) {
currentPageSize = defaultPageSize;
}
}
if (page.HasValue) {
currentPage = page.Value+1;
if (currentPage < 1) {
currentPage = 1;
}
}
// using (repo.DisableTracking())
{
IQueryable<PhbkDivision> query = (await repo.GetQueryableAsync()); // .PhbkDivisionDbSet;
int _id = id == null ? 0 : id.Length;
if (_id > 0) {
int _idOprtr = idOprtr == null ? 0 : idOprtr.Length;
for(int i = 0; i < _id; i++) {
string op_idOprtr = (i >= _idOprtr) ? "eq" : (idOprtr[i] == null) ? "eq" : idOprtr[i];
var _tmpid = id[i];
switch(op_idOprtr) {
case "eq":
query = query.Where(p => (p.Id == _tmpid));
break;
case "lk":
case "cn":
case "sw":
query = query.Where(p => (p.Id == _tmpid));
break;
case "gt":
query = query.Where(p => (p.Id >= _tmpid));
break;
case "lt":
query = query.Where(p => (p.Id <= _tmpid));
break;
case "ne":
query = query.Where(p => (p.Id != _tmpid));
break;
}
}
}
int _divisionName = divisionName == null ? 0 : divisionName.Length;
if (_divisionName > 0) {
int _divisionNameOprtr = divisionNameOprtr == null ? 0 : divisionNameOprtr.Length;
for(int i = 0; i < _divisionName; i++) {
string op_divisionNameOprtr = (i >= _divisionNameOprtr) ? "eq" : (divisionNameOprtr[i] == null) ? "eq" : divisionNameOprtr[i];
var _tmpdivisionName = divisionName[i];
switch(op_divisionNameOprtr) {
case "eq":
if (string.IsNullOrEmpty(_tmpdivisionName)) {
query = query.Where(p => p.DivisionName == null);
} else {
query = query.Where(p => p.DivisionName == _tmpdivisionName);
}
break;
case "sw":
if (!string.IsNullOrEmpty(_tmpdivisionName)) {
query = query.Where(p => p.DivisionName.StartsWith(_tmpdivisionName));
}
break;
case "lk":
case "cn":
query = query.Where(p => p.DivisionName.Contains(_tmpdivisionName));
break;
case "gt":
query = query.Where(p => (p.DivisionName.CompareTo(_tmpdivisionName) >= 0));
break;
case "lt":
query = query.Where(p => (p.DivisionName.CompareTo(_tmpdivisionName) <= 0));
break;
case "ne":
query = query.Where(p => (p.DivisionName.CompareTo(_tmpdivisionName) != 0));
break;
}
}
}
int _entrprsIdRef = entrprsIdRef == null ? 0 : entrprsIdRef.Length;
if (_entrprsIdRef > 0) {
int _entrprsIdRefOprtr = entrprsIdRefOprtr == null ? 0 : entrprsIdRefOprtr.Length;
for(int i = 0; i < _entrprsIdRef; i++) {
string op_entrprsIdRefOprtr = (i >= _entrprsIdRefOprtr) ? "eq" : (entrprsIdRefOprtr[i] == null) ? "eq" : entrprsIdRefOprtr[i];
var _tmpentrprsIdRef = entrprsIdRef[i];
switch(op_entrprsIdRefOprtr) {
case "eq":
query = query.Where(p => (p.EntrprsIdRef == _tmpentrprsIdRef));
break;
case "lk":
case "cn":
case "sw":
query = query.Where(p => (p.EntrprsIdRef == _tmpentrprsIdRef));
break;
case "gt":
query = query.Where(p => (p.EntrprsIdRef >= _tmpentrprsIdRef));
break;
case "lt":
query = query.Where(p => (p.EntrprsIdRef <= _tmpentrprsIdRef));
break;
case "ne":
query = query.Where(p => (p.EntrprsIdRef != _tmpentrprsIdRef));
break;
}
}
}
// int totals = await query.CountAsync();
int totals = await AsyncExecuter.CountAsync( // .GetCountAsync(
query);
int pageCount = ((totals > 0) ? ((int)Math.Ceiling((double)totals / (double)currentPageSize)) : 0);
List<string> currentOrderBy = null;
if (orderby != null) {
if (orderby.Length > 0) {
currentOrderBy = orderby.Where(s => (!string.IsNullOrEmpty(s))).ToList();
}
}
bool isFirstTime = true;
IOrderedQueryable<PhbkDivision> orderedQuery = null;
if(currentOrderBy != null) {
List<string> wasInUseOrderBy = new List<string>();
foreach(string propName in currentOrderBy) {
string lowerCaseStr = propName.ToLower();
if (wasInUseOrderBy.Contains(lowerCaseStr)) {
continue;
}
switch(lowerCaseStr) {
case "id" :
if(isFirstTime) {
orderedQuery = query.OrderBy(p => p.Id);
isFirstTime = false;
} else {
orderedQuery = orderedQuery.ThenBy(p => p.Id);
}
wasInUseOrderBy.Add("id");
wasInUseOrderBy.Add("-id");
break;
case "-id" :
if(isFirstTime) {
orderedQuery = query.OrderByDescending(p => p.Id);
isFirstTime = false;
} else {
orderedQuery = orderedQuery.ThenByDescending(p => p.Id);
}
wasInUseOrderBy.Add("id");
wasInUseOrderBy.Add("-id");
break;
case "divisionname" :
if(isFirstTime) {
orderedQuery = query.OrderBy(p => p.DivisionName);
isFirstTime = false;
} else {
orderedQuery = orderedQuery.ThenBy(p => p.DivisionName);
}
wasInUseOrderBy.Add("divisionname");
wasInUseOrderBy.Add("-divisionname");
break;
case "-divisionname" :
if(isFirstTime) {
orderedQuery = query.OrderByDescending(p => p.DivisionName);
isFirstTime = false;
} else {
orderedQuery = orderedQuery.ThenByDescending(p => p.DivisionName);
}
wasInUseOrderBy.Add("divisionname");
wasInUseOrderBy.Add("-divisionname");
break;
case "entrprsidref" :
if(isFirstTime) {
orderedQuery = query.OrderBy(p => p.EntrprsIdRef);
isFirstTime = false;
} else {
orderedQuery = orderedQuery.ThenBy(p => p.EntrprsIdRef);
}
wasInUseOrderBy.Add("entrprsidref");
wasInUseOrderBy.Add("-entrprsidref");
break;
case "-entrprsidref" :
if(isFirstTime) {
orderedQuery = query.OrderByDescending(p => p.EntrprsIdRef);
isFirstTime = false;
} else {
orderedQuery = orderedQuery.ThenByDescending(p => p.EntrprsIdRef);
}
wasInUseOrderBy.Add("entrprsidref");
wasInUseOrderBy.Add("-entrprsidref");
break;
default:
break;
}
}
}
if(isFirstTime) {
orderedQuery = query.OrderBy(p => p.Id);
} // totals pageCount currentPageSize
PhbkDivisionDtoPage resultObject = new PhbkDivisionDtoPage() {
page = (currentPage > 0) ? (currentPage-1) : currentPage,
pagesize = currentPageSize,
pagecount = pageCount,
total = totals
};
resultObject.items = await AsyncExecuter.ToListAsync( // .GetListAsync(
orderedQuery.Skip((currentPage - 1) * currentPageSize).Take(currentPageSize).Select(itm => new PhbkDivisionDto() {
Id = itm.Id
, DivisionName = itm.DivisionName
, DivisionDesc = itm.DivisionDesc
, ConcurrencyStamp = itm.ConcurrencyStamp
, EntrprsIdRef = itm.EntrprsIdRef
}) ); //.ToListAsync();
return resultObject;
} // using (repo.DisableTracking()) { ... }
} // the end of GetWithFilter()-method
public async Task<PhbkDivisionDto> getone(
System.Int32 id
)
{
// using (repo.DisableTracking())
{
var appQr = (await repo.GetQueryableAsync()) // .PhbkDivisionDbSet
.Where(p => p.Id == id)
.Select(itm => new PhbkDivisionDto() {
Id = itm.Id
, DivisionName = itm.DivisionName
, DivisionDesc = itm.DivisionDesc
, ConcurrencyStamp = itm.ConcurrencyStamp
, EntrprsIdRef = itm.EntrprsIdRef
}); // .FirstOrDefaultAsync();
return await AsyncExecuter.FirstOrDefaultAsync(appQr);
} // using (repo.DisableTracking()) { ... }
} // the end of public GetOne()-method
public async Task<PhbkDivisionDtoPage> getmanybyrepprim(
System.Int32?[] id,
string[] idOprtr,
System.Int32?[] entrprsIdRef,
string[] entrprsIdRefOprtr,
string[] orderby = null,
int? page =null,
int? pagesize = null)
{
int currentPageSize = this.defaultPageSize;
int currentPage = 1;
if (pagesize.HasValue) {
currentPageSize = pagesize.Value;
if ((currentPageSize < this.minPageSize) || (currentPageSize > this.maxPageSize)) {
currentPageSize = defaultPageSize;
}
}
if (page.HasValue) {
currentPage = page.Value+1;
if (currentPage < 1) {
currentPage = 1;
}
}
int _id = id == null ? 0 : id.Length;
int _idOprtr = idOprtr == null ? 0 : idOprtr.Length;
// using (repo.DisableTracking())
{
IQueryable<PhbkDivision> query = (await repo.GetQueryableAsync()); //.PhbkDivisionDbSet;
var _outer = PredicateBuilder.New<PhbkDivision>(false);
bool isOuterModified = false;
if ( _id > 0 ) {
bool isLkOp = false;
for(int i = 0; i < _id; i++) {
var _inner = PredicateBuilder.New<PhbkDivision>(true);
isLkOp = false;
if(i < _idOprtr) {
isLkOp = idOprtr[i] == "lk";
}
if (id[i] == null) // continue; (required prop == null) returns false
{
continue;
}
var _tmpid = id[i];
_inner = _inner.And(p => p.Id == _tmpid);
_outer = _outer.Or(_inner);
isOuterModified = true;
}
}
bool isForeignAdded = false;
var _outerAnd = PredicateBuilder.New<PhbkDivision>(true);
if(entrprsIdRef != null) {
if(entrprsIdRef.Length > 0) {
for(int i = 0; i < entrprsIdRef.Length; i++) {
if (entrprsIdRef[i] == null) continue; // (required prop == null) returns false
var _tmpentrprsIdRef = entrprsIdRef[i];
_outerAnd = _outerAnd.And(p => p.EntrprsIdRef == _tmpentrprsIdRef);
isForeignAdded = true;
}
}
}
if(isForeignAdded) {
if(isOuterModified) {
_outer = _outerAnd.And(_outer);
} else {
_outer = _outerAnd;
}
isOuterModified = true;
}
if(isOuterModified) {
query = query.AsExpandable().Where(_outer);
}
// int totals = await query.CountAsync();
int totals = await AsyncExecuter.CountAsync( // .GetCountAsync(
query);
int pageCount = ((totals > 0) ? ((int)Math.Ceiling((double)totals / (double)currentPageSize)) : 0);
IOrderedQueryable<PhbkDivision> orderedQuery = null;
if(!isForeignAdded)
{
orderedQuery = query.OrderBy(p => p.Id);
query = orderedQuery;
}
PhbkDivisionDtoPage resultObject = new PhbkDivisionDtoPage() {
page = (currentPage > 0) ? (currentPage-1) : currentPage,
pagesize = currentPageSize,
pagecount = pageCount,
total = totals
};
resultObject.items = await AsyncExecuter.ToListAsync( // .GetListAsync(
query.Skip((currentPage - 1) * currentPageSize).Take(currentPageSize).Select(itm => new PhbkDivisionDto() {
Id = itm.Id
, DivisionName = itm.DivisionName
, DivisionDesc = itm.DivisionDesc
, ConcurrencyStamp = itm.ConcurrencyStamp
, EntrprsIdRef = itm.EntrprsIdRef
})); //.ToListAsync();
return resultObject;
} // using (repo.DisableTracking()) { ... }
}
[Authorize(firstappPermissions.PhbkDivisionDto.Edit)]
public async Task<PhbkDivisionDto?> updateone(PhbkDivisionDto viewToUpdate)
{
// using(repo.EnableTracking())
{
var appQrOld = (await repo.GetQueryableAsync()) // .PhbkDivisionDbSet
.Where(p => p.Id == viewToUpdate.Id)
.Select(itm => new PhbkDivisionDto() {
Id = itm.Id
, DivisionName = itm.DivisionName
, DivisionDesc = itm.DivisionDesc
, ConcurrencyStamp = itm.ConcurrencyStamp
, EntrprsIdRef = itm.EntrprsIdRef
});
PhbkDivisionDto oldObjVer = await AsyncExecuter.FirstOrDefaultAsync(appQrOld);
var appQr = (await repo.GetQueryableAsync()) // .PhbkDivisionDbSet
.Where(p => p.Id == viewToUpdate.Id)
;
PhbkDivision resultEntity = await AsyncExecuter.FirstOrDefaultAsync(appQr);
if(resultEntity == null) {
return null;
}
resultEntity.ChangeVals(
viewToUpdate.DivisionName
, viewToUpdate.DivisionDesc
, viewToUpdate.EntrprsIdRef
, viewToUpdate.ConcurrencyStamp
);
await repo.UpdateAsync(resultEntity);
await UnitOfWorkManager.Current.SaveChangesAsync();
var appQrEx = (await repo.GetQueryableAsync()) // .PhbkDivisionDbSet
.Where(p => p.Id == viewToUpdate.Id)
.Select(itm => new PhbkDivisionDto() {
Id = itm.Id
, DivisionName = itm.DivisionName
, DivisionDesc = itm.DivisionDesc
, ConcurrencyStamp = itm.ConcurrencyStamp
, EntrprsIdRef = itm.EntrprsIdRef
});
PhbkDivisionDto retResult = await AsyncExecuter.FirstOrDefaultAsync(appQrEx);
if (retResult == null)
{
return null;
}
PhbkDivisionEto newcln = PhbkDivisionDtoCloneForLkUp.DoClone(retResult);
PhbkDivisionEto oldcln = PhbkDivisionDtoCloneForLkUp.DoClone(oldObjVer);
//
// Please define additional props of the PhbkDivisionEto newcln object before UpdateForXXX-method calls
//
await _distributedEventBus.PublishAsync(new PhbkDivisionEtoEx()
{
TenantId = CurrentTenant.Id,
Action = 2,
OldVals = oldcln,
NewVals = newcln
});
return retResult;
} // using(repo.EnableTracking()) { ... }
}
[Authorize(firstappPermissions.PhbkDivisionDto.Create)]
public async Task<PhbkDivisionDto> addone(PhbkDivisionDto viewToAdd)
{
// using(repo.EnableTracking())
{
PhbkDivision entityToAdd = new PhbkDivision(
viewToAdd.Id
, viewToAdd.DivisionName
, viewToAdd.DivisionDesc
, viewToAdd.EntrprsIdRef
);
await repo.InsertAsync(entityToAdd);
await UnitOfWorkManager.Current.SaveChangesAsync();
var appQr = (await repo.GetQueryableAsync())
.Where(p => p.Id == entityToAdd.Id)
.Select(itm => new PhbkDivisionDto() {
Id = itm.Id
, DivisionName = itm.DivisionName
, DivisionDesc = itm.DivisionDesc
, ConcurrencyStamp = itm.ConcurrencyStamp
, EntrprsIdRef = itm.EntrprsIdRef
});
PhbkDivisionDto reRsult = await AsyncExecuter.FirstOrDefaultAsync(appQr);
if (reRsult == null)
{
return reRsult;
}
PhbkDivisionEto newcln = PhbkDivisionDtoCloneForLkUp.DoClone(reRsult);
//
// Please define additional props of the PhbkDivisionEto newcln object before UpdateForXXX-method calls
//
await _distributedEventBus.PublishAsync(new PhbkDivisionEtoEx()
{
TenantId = CurrentTenant.Id,
Action = 1,
// OldVals = null,
NewVals = newcln
});
return reRsult;
} // using(repo.EnableTracking()) { ... }
}
[Authorize(firstappPermissions.PhbkDivisionDto.Delete)]
public async Task<PhbkDivisionDto> deleteone(
System.Int32 id
)
{
// using(repo.EnableTracking())
{
var appQr = (await repo.GetQueryableAsync())
.Where(p => p.Id == id)
.Select(itm => new PhbkDivisionDto() {
Id = itm.Id
, DivisionName = itm.DivisionName
, DivisionDesc = itm.DivisionDesc
, ConcurrencyStamp = itm.ConcurrencyStamp
, EntrprsIdRef = itm.EntrprsIdRef
});
PhbkDivisionDto result = await AsyncExecuter.FirstOrDefaultAsync(appQr);
if (result == null)
{
return null;
}
var appQrEx = (await repo.GetQueryableAsync())
.Where(p => p.Id == result.Id)
;
PhbkDivision entityToDelete = await AsyncExecuter.FirstOrDefaultAsync(appQrEx);
if (entityToDelete == null) {
return result;
}
await repo.DeleteAsync(entityToDelete);
await UnitOfWorkManager.Current.SaveChangesAsync();
PhbkDivisionEto oldcln = PhbkDivisionDtoCloneForLkUp.DoClone(result);
//
// Please define additional props of the PhbkDivisionEto oldcln object before UpdateForXXX-method calls
//
await _distributedEventBus.PublishAsync(new PhbkDivisionEtoEx()
{
TenantId = CurrentTenant.Id,
Action = 3,
OldVals = oldcln
// NewVals = null
});
return result;
} // using(repo.EnableTracking()) { ... }
}
}
}