on the server too

This commit is contained in:
2025-11-09 16:40:29 +11:00
parent 2358ac9090
commit 502945ca2b
6 changed files with 152 additions and 73 deletions
+5
View File
@@ -1,5 +1,10 @@
namespace FamilyTreeAPI.Entities; namespace FamilyTreeAPI.Entities;
public class TreeData
{
public string? Sex { get; set; }
public string? Image { get; set; }
}
public class TreeNode<T> public class TreeNode<T>
{ {
public string? Label { get; set; } public string? Label { get; set; }
+2 -2
View File
@@ -4,8 +4,8 @@ namespace FamilyTreeAPI.Interface;
public interface IPerson public interface IPerson
{ {
Task<ResultModel<TreeNode<string>>> GetByFamilyAsync(int id); Task<ResultModel<TreeNode<TreeData>>> GetByFamilyAsync(int id);
Task<ResultModel<List<TreeNode<string>>>> GetFamilyTreeBy(FamilyCriteria criteria); Task<ResultModel<List<TreeNode<TreeData>>>> GetFamilyTreeBy(FamilyCriteria criteria);
Task<ResultModel<List<PersonDto>>> GetChildren(ChildCriteria criteria); Task<ResultModel<List<PersonDto>>> GetChildren(ChildCriteria criteria);
Task<ResultModel<List<PersonDto>>> GetPerson(PersonCriteria criteria); Task<ResultModel<List<PersonDto>>> GetPerson(PersonCriteria criteria);
Task<ResultModel<Dictionary<int,PersonDto>>> GetDicFamily(); Task<ResultModel<Dictionary<int,PersonDto>>> GetDicFamily();
@@ -22,10 +22,13 @@ public partial class PersonRepository : IPerson
private readonly IHttpContextAccessor _httpContext; private readonly IHttpContextAccessor _httpContext;
private readonly IPersonPhoto _personPhoto; private readonly IPersonPhoto _personPhoto;
private readonly IConfiguration _config; private readonly IConfiguration _config;
private readonly ImportPersonRepository _importPersonRepository;
const string dateFormat = "yyyy-MM-dd"; const string dateFormat = "yyyy-MM-dd";
public PersonRepository(IConfiguration config, FamilyTreeDBContext context, public PersonRepository(IConfiguration config, FamilyTreeDBContext context,
IRelationShipd relationship, IRelationShipd relationship,
IPersonPhoto personPhoto, IPersonPhoto personPhoto,
ImportPersonRepository importPersonRepository,
IHttpContextAccessor httpContext) IHttpContextAccessor httpContext)
{ {
_personPhoto = personPhoto; _personPhoto = personPhoto;
@@ -33,6 +36,7 @@ public partial class PersonRepository : IPerson
_relationship = relationship; _relationship = relationship;
_config = config; _config = config;
_httpContext = httpContext; _httpContext = httpContext;
_importPersonRepository = importPersonRepository;
} }
private PersonDto FillDto(Person model) private PersonDto FillDto(Person model)
@@ -59,6 +63,7 @@ public partial class PersonRepository : IPerson
return dto; return dto;
} }
private Person FillModel(Person model, PersonDto dto) private Person FillModel(Person model, PersonDto dto)
{ {
@@ -93,8 +98,9 @@ public partial class PersonRepository : IPerson
{ {
jitem = jlist[i]; jitem = jlist[i];
item = FillDto(jitem); item = FillDto(jitem);
list.Add(item.Id,item); list.Add(item.Id, item);
} }
statuscode = 1; statuscode = 1;
//list.Sort((x, y) => x.Code.CompareTo(y.Code)); //list.Sort((x, y) => x.Code.CompareTo(y.Code));
} }
@@ -105,7 +111,8 @@ public partial class PersonRepository : IPerson
statuscode = -1; statuscode = -1;
} }
return new ResultModel< Dictionary<int, PersonDto> > ()
return new ResultModel<Dictionary<int, PersonDto>>()
{ {
Data = list, Data = list,
StatusCode = statuscode, StatusCode = statuscode,
@@ -127,8 +134,10 @@ public partial class PersonRepository : IPerson
item = FillDto(jitem); item = FillDto(jitem);
list.Add(item); list.Add(item);
} }
return list; return list;
} }
public async Task<ResultModel<List<PersonDto>>> GetChildren(ChildCriteria criteria) public async Task<ResultModel<List<PersonDto>>> GetChildren(ChildCriteria criteria)
{ {
List<PersonDto> list = new(); List<PersonDto> list = new();
@@ -150,6 +159,7 @@ public partial class PersonRepository : IPerson
statuscode = -1; statuscode = -1;
} }
return new ResultModel<List<PersonDto>>() return new ResultModel<List<PersonDto>>()
{ {
Data = list, Data = list,
@@ -177,14 +187,15 @@ public partial class PersonRepository : IPerson
{ {
firstName = firstName.ToLower(); firstName = firstName.ToLower();
jlist = await _context.Persons.Where(x => jlist = await _context.Persons.Where(x =>
EF.Functions.Like(x.FirstName.ToLower(),firstName + "%")).ToListAsync(); EF.Functions.Like(x.FirstName.ToLower(), firstName + "%")).ToListAsync();
} }
else if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName)) else if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName))
{ {
firstName = firstName.ToLower(); firstName = firstName.ToLower();
lastName = lastName.ToLower(); lastName = lastName.ToLower();
jlist = await _context.Persons.Where(x => EF.Functions.Like(x.FirstName.ToLower(),firstName + "%") jlist = await _context.Persons.Where(x => EF.Functions.Like(x.FirstName.ToLower(), firstName + "%")
&& EF.Functions.Like(x.LastName.ToLower(),lastName + "%")).ToListAsync(); && EF.Functions.Like(x.LastName.ToLower(), lastName + "%"))
.ToListAsync();
} }
else else
jlist = await _context.Persons.ToListAsync(); jlist = await _context.Persons.ToListAsync();
@@ -195,6 +206,7 @@ public partial class PersonRepository : IPerson
item = FillDto(jitem); item = FillDto(jitem);
list.Add(item); list.Add(item);
} }
statuscode = 1; statuscode = 1;
//list.Sort((x, y) => x.Code.CompareTo(y.Code)); //list.Sort((x, y) => x.Code.CompareTo(y.Code));
} }
@@ -205,6 +217,7 @@ public partial class PersonRepository : IPerson
statuscode = -1; statuscode = -1;
} }
return new ResultModel<List<PersonDto>>() return new ResultModel<List<PersonDto>>()
{ {
Data = list, Data = list,
@@ -237,9 +250,11 @@ public partial class PersonRepository : IPerson
{ {
result = "bg-teal-500 text-white"; result = "bg-teal-500 text-white";
} }
return result; return result;
} }
public async Task<ResultModel<TreeNode<string>>> GetByFamilyAsync(int id)
public async Task<ResultModel<TreeNode<TreeData>>> GetByFamilyAsync(int id)
{ {
int statuscode = 0; int statuscode = 0;
string error = ""; string error = "";
@@ -248,9 +263,10 @@ public partial class PersonRepository : IPerson
string type = "default"; string type = "default";
string pName = ""; string pName = "";
Person person; Person person;
TreeNode<string> citem; TreeNode<TreeData> citem;
TreeNode<string> child; TreeNode<TreeData> child;
TreeNode<string> node = new(); TreeNode<TreeData> node = new();
node.Data = new();
node.Children = new(); node.Children = new();
PersonDto dto = new(); PersonDto dto = new();
try try
@@ -266,10 +282,13 @@ public partial class PersonRepository : IPerson
node.Label = item.FirstName; node.Label = item.FirstName;
node.Key = item.Id.ToString(); node.Key = item.Id.ToString();
node.Type = type; node.Type = type;
if (item.Image != null)
node.Data.Image = await LoadPersonImage(item.Image);
node.Icon = "T"; node.Icon = "T";
node.StyleClass = getStyleClass(node.Icon); node.StyleClass = getStyleClass(node.Icon);
node.Expanded = true; node.Expanded = true;
node.Data = item.Sex; node.Data.Sex = item.Sex;
statuscode = 1; statuscode = 1;
dto = FillDto(item); dto = FillDto(item);
await GetPartnerChildrens(dto, node); await GetPartnerChildrens(dto, node);
@@ -281,7 +300,8 @@ public partial class PersonRepository : IPerson
statuscode = -1; statuscode = -1;
} }
return new ResultModel<TreeNode<string>> ()
return new ResultModel<TreeNode<TreeData>>()
{ {
Data = node, Data = node,
StatusCode = statuscode, StatusCode = statuscode,
@@ -289,21 +309,22 @@ public partial class PersonRepository : IPerson
}; };
} }
private async Task GetPartnerChildrens(PersonDto person, TreeNode<string> node) private async Task GetPartnerChildrens(PersonDto person, TreeNode<TreeData> node)
{ {
/*****************************/ /*****************************/
ResultModel<List<RelationShipDto>> rlist = await _relationship.GetByPersonIdAsync(person.Id); ResultModel<List<RelationShipDto>> rlist = await _relationship.GetByPersonIdAsync(person.Id);
List<RelationShipDto> relationShips = rlist.Data; List<RelationShipDto> relationShips = rlist.Data;
RelationShipDto relate; RelationShipDto relate;
string type = node.Type; string type = node.Type;
TreeNode<string> child; TreeNode<TreeData> child;
PersonDto dto; PersonDto dto;
Person pe; Person pe;
int fatherId, motherId; int fatherId, motherId;
fatherId = motherId = 0; fatherId = motherId = 0;
string data = person.Sex; string data = person.Sex;
TreeNode<string> citem; TreeNode<TreeData> citem;
string pName = ""; string pName = "";
string? image = "";
string key = ""; string key = "";
for (int i = 0; i < relationShips.Count; i++) for (int i = 0; i < relationShips.Count; i++)
{ {
@@ -336,6 +357,7 @@ public partial class PersonRepository : IPerson
motherId = relate.RelatePersonId; motherId = relate.RelatePersonId;
} }
} }
//get children //get children
List<PersonDto> children = await GetChildrens(fatherId, motherId); List<PersonDto> children = await GetChildrens(fatherId, motherId);
if (children.Count > 0) if (children.Count > 0)
@@ -347,6 +369,7 @@ public partial class PersonRepository : IPerson
{ {
pe = await GetPerson(motherId); pe = await GetPerson(motherId);
pName = pe.FirstName; pName = pe.FirstName;
image = pe.Image;
key = motherId.ToString(); key = motherId.ToString();
data = pe.Sex; data = pe.Sex;
} }
@@ -357,32 +380,43 @@ public partial class PersonRepository : IPerson
{ {
pe = await GetPerson(fatherId); pe = await GetPerson(fatherId);
pName = pe.FirstName; pName = pe.FirstName;
image = pe.Image;
key = fatherId.ToString(); key = fatherId.ToString();
data = pe.Sex; data = pe.Sex;
} }
} }
citem = new TreeNode<string>(); citem = new TreeNode<TreeData>();
citem.Data = new();
citem.Label = pName; citem.Label = pName;
if (image != null)
citem.Data.Image = await LoadPersonImage(image);
citem.Icon = "P"; citem.Icon = "P";
// citem.Icon = "P";
citem.StyleClass = getStyleClass(citem.Icon); citem.StyleClass = getStyleClass(citem.Icon);
citem.Expanded = true; citem.Expanded = true;
citem.Data = data; citem.Data.Sex = data;
citem.Type = type; citem.Type = type;
citem.Key = key; citem.Key = key;
citem.Children = new List<TreeNode<string>>(); citem.Children = new List<TreeNode<TreeData>>();
node.Children.Add(citem); node.Children.Add(citem);
for (int j = 0; j < children.Count; j++) for (int j = 0; j < children.Count; j++)
{ {
dto = children[j]; dto = children[j];
child = new TreeNode<string>(); child = new TreeNode<TreeData>();
child.Data = new();
child.Expanded = true; child.Expanded = true;
child.Type = type; child.Type = type;
image = dto.Image;
if (image != null)
child.Data.Image = await LoadPersonImage(image);
child.Icon = "C"; child.Icon = "C";
child.StyleClass = getStyleClass(child.Icon); child.StyleClass = getStyleClass(child.Icon);
child.Label = dto.FirstName; child.Label = dto.FirstName;
child.Key = dto.Id.ToString(); child.Key = dto.Id.ToString();
child.Data = dto.Sex; child.Data.Sex = dto.Sex;
child.Children = new(); child.Children = new();
citem.Children.Add(child); citem.Children.Add(child);
//get child partner and repeat this. //get child partner and repeat this.
@@ -425,6 +459,7 @@ public partial class PersonRepository : IPerson
statuscode = -1; statuscode = -1;
} }
return new ResultModel<PersonDto>() return new ResultModel<PersonDto>()
{ {
Data = dto, Data = dto,
@@ -432,6 +467,7 @@ public partial class PersonRepository : IPerson
Message = error Message = error
}; };
} }
private string GetDateTimeNow() private string GetDateTimeNow()
{ {
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
@@ -453,7 +489,7 @@ public partial class PersonRepository : IPerson
string sdate = GetCurrentDateTime(); string sdate = GetCurrentDateTime();
string filename, extention; string filename, extention;
filename = extention = ""; filename = extention = "";
int first = base64.IndexOf("base64,") +"base64,".Length; int first = base64.IndexOf("base64,") + "base64,".Length;
int last = base64.Length; int last = base64.Length;
string rbase = base64.Substring(first, last - first); string rbase = base64.Substring(first, last - first);
@@ -463,6 +499,7 @@ public partial class PersonRepository : IPerson
{ {
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
} }
extention = System.IO.Path.GetExtension(fileName); extention = System.IO.Path.GetExtension(fileName);
filename = familyId + "_" + sdate + extention; filename = familyId + "_" + sdate + extention;
string fullpath = System.IO.Path.Combine(path, filename); string fullpath = System.IO.Path.Combine(path, filename);
@@ -490,6 +527,7 @@ public partial class PersonRepository : IPerson
return filename; return filename;
} }
public async Task<ResultModel<int>> SaveAsync(PersonForSave container) public async Task<ResultModel<int>> SaveAsync(PersonForSave container)
{ {
int result = default(int); int result = default(int);
@@ -504,6 +542,7 @@ public partial class PersonRepository : IPerson
if (user != null) if (user != null)
loginName = user.FirstName + " " + user.LastName; loginName = user.FirstName + " " + user.LastName;
} }
PersonDto item = container.Person; PersonDto item = container.Person;
try try
{ {
@@ -544,6 +583,7 @@ public partial class PersonRepository : IPerson
// update the image in table to new // update the image in table to new
model1.Image = image; model1.Image = image;
} }
var successid = await _context.SaveChangesAsync(); var successid = await _context.SaveChangesAsync();
} }
// model.LastModified = DateTime.Now; // model.LastModified = DateTime.Now;
@@ -563,6 +603,7 @@ public partial class PersonRepository : IPerson
error = ex.ToString(); error = ex.ToString();
statuscode = -1; statuscode = -1;
} }
//var dto = await Task.Run(() => result); //var dto = await Task.Run(() => result);
return new ResultModel<int>() return new ResultModel<int>()
{ {
@@ -593,6 +634,7 @@ public partial class PersonRepository : IPerson
statuscode = -1; statuscode = -1;
} }
//var dto = await Task.Run(() => result); //var dto = await Task.Run(() => result);
return new ResultModel<int>() return new ResultModel<int>()
{ {
@@ -602,7 +644,8 @@ public partial class PersonRepository : IPerson
}; };
} }
public Task<ResultModel<string>> UploadImage(UploadCriteria criteria) { public Task<ResultModel<string>> UploadImage(UploadCriteria criteria)
{
return UploadImagep(criteria); return UploadImagep(criteria);
} }
@@ -630,6 +673,7 @@ public partial class PersonRepository : IPerson
{ {
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
} }
extention = System.IO.Path.GetExtension(criteria.FileName); extention = System.IO.Path.GetExtension(criteria.FileName);
filename = criteria.PersonId + "_" + sdate + extention; filename = criteria.PersonId + "_" + sdate + extention;
@@ -662,6 +706,17 @@ public partial class PersonRepository : IPerson
}; };
} }
private async Task<string> LoadPersonImage(string filename)
{
string result = "";
DownloadFileCriteria criteria = new() { FileName = filename };
var resultModel = await _importPersonRepository.DownloadFile(criteria);
if (!string.IsNullOrEmpty((resultModel.Data)))
result = "data:image/png;base64," + resultModel.Data;
return result;
}
public ResultModel<int> DeleteUploadFile(DeleteFileCriteria criteria) public ResultModel<int> DeleteUploadFile(DeleteFileCriteria criteria)
{ {
int result = -1; int result = -1;
@@ -688,6 +743,7 @@ public partial class PersonRepository : IPerson
statusCode = -1; statusCode = -1;
error += e.Message; error += e.Message;
} }
return new ResultModel<int>() return new ResultModel<int>()
{ {
Data = result, Data = result,
@@ -696,3 +752,4 @@ public partial class PersonRepository : IPerson
}; };
} }
} }
@@ -6,11 +6,12 @@ namespace FamilyTreeAPI.Repository;
public partial class PersonRepository public partial class PersonRepository
{ {
private TreeNode<string> PopulateItem(Person model) private TreeNode<TreeData> PopulateItem(Person model)
{ {
TreeNode<string> treeNode = new(); TreeNode<TreeData> treeNode = new();
treeNode.Data = new();
treeNode.Label = model.FirstName; treeNode.Label = model.FirstName;
treeNode.Data = model.Id.ToString(); // treeNode.Key= model.Id.ToString();
treeNode.Key = model.Id.ToString(); treeNode.Key = model.Id.ToString();
treeNode.Expanded = false; treeNode.Expanded = false;
treeNode.Children = new(); treeNode.Children = new();
@@ -86,11 +87,11 @@ public partial class PersonRepository
return result; return result;
} }
private List<TreeNode<string>> PopulateChild(FamilyCriteria criteria,int id, List<Person> childList, Func<FamilyCriteria, int ,Person,bool> conditionFn) private List<TreeNode<TreeData>> PopulateChild(FamilyCriteria criteria,int id, List<Person> childList, Func<FamilyCriteria, int ,Person,bool> conditionFn)
{ {
Person person; Person person;
TreeNode<string> treeNode; TreeNode<TreeData> treeNode;
List<TreeNode<string>> list = new(); List<TreeNode<TreeData>> list = new();
List<Person> children = GetParentId(childList,id, criteria, conditionFn); List<Person> children = GetParentId(childList,id, criteria, conditionFn);
for (int i = 0; i < children.Count; i++) for (int i = 0; i < children.Count; i++)
{ {
@@ -102,13 +103,13 @@ public partial class PersonRepository
} }
return list; return list;
} }
public async Task<ResultModel<List<TreeNode<string>>> > GetFamilyTreeBy(FamilyCriteria criteria) public async Task<ResultModel<List<TreeNode<TreeData>>> > GetFamilyTreeBy(FamilyCriteria criteria)
{ {
int statusCode = -1; int statusCode = -1;
string error = ""; string error = "";
Person person; Person person;
TreeNode<string> treeNode; TreeNode<TreeData> treeNode;
List<TreeNode<string>> data = new(); List<TreeNode<TreeData>> data = new();
try try
{ {
var personList = await _context.Persons.ToListAsync(); var personList = await _context.Persons.ToListAsync();
@@ -129,7 +130,7 @@ public partial class PersonRepository
statusCode = -1; statusCode = -1;
} }
return new ResultModel<List<TreeNode<string>>> return new ResultModel<List<TreeNode<TreeData>>>
{ {
Data = data, Data = data,
StatusCode = statusCode, StatusCode = statusCode,
+16
View File
@@ -249,3 +249,19 @@ begin
; ;
end; end;
$BODY$; $BODY$;
kham
v2
CREATE TABLE IF NOT EXISTS "personphoto" (
"id" serial NOT NULL,
"personid" INTEGER NOT NULL,
"photo" TEXT NOT NULL,
"imagephot" BYTEA NOT NULL,
"phototype" VARCHAR(20) NULL DEFAULT NULL,
PRIMARY KEY ("id")
);
ALTER TABLE personphoto
alter column id ADD GENERATED BY DEFAULT AS IDENTITY ;
+2 -2
View File
@@ -2,8 +2,8 @@
"AppSettings": { "AppSettings": {
"Secret": "Nepean Blue Mountain Super Secret SIGN AND VERIFY JWT TOKENS, BEARER TOKEN USE WHEN CALLING THIS API.", "Secret": "Nepean Blue Mountain Super Secret SIGN AND VERIFY JWT TOKENS, BEARER TOKEN USE WHEN CALLING THIS API.",
"SQLConnectionString": "host=localhost;port=5432;database=FamilyTreeDB;username=postgres;password=Positive~1;", "SQLConnectionString_o": "host=localhost;port=5432;database=FamilyTreeDB;username=postgres;password=Positive~1;",
"SQLConnectionString_25": "host=192.168.1.100;port=5432;database=FamilyTreeDB;username=postgres;password=Positive~1;", "SQLConnectionString": "host=192.168.1.123;port=5432;database=FamilyTreeDB;username=postgres;password=Positive~1;",
"ImageFolder": "c:\\temp\\Family", "ImageFolder": "c:\\temp\\Family",
"ImportFolder": "c:\\temp" "ImportFolder": "c:\\temp"