put in new ui and backend store image to table

This commit is contained in:
2026-09-20 14:32:59 +10:00
parent 75b49be476
commit 3d83e95613
13 changed files with 194 additions and 55 deletions
@@ -3,6 +3,7 @@ using FamilyTreeAPI.Entities;
using FamilyTreeAPI.Helper;
using FamilyTreeAPI.Interface;
using FamilyTreeAPI.Models;
using HotChocolate.CostAnalysis.Types;
using Microsoft.EntityFrameworkCore;
namespace FamilyTreeAPI.Repository
@@ -31,12 +32,12 @@ namespace FamilyTreeAPI.Repository
dto.PhotoType = model.PhotoType;
return dto;
}
private PersonPhoto FillModel(PersonPhoto model, int personId, string filename)
private PersonPhoto FillModel(PersonPhoto model, int personId, int photoType, string filename)
{
model.PersonId = personId;
model.Photo = filename;
model.PhotoType = System.IO.Path.GetExtension(filename);
model.PhotoType = photoType.ToString(); //System.IO.Path.GetExtension(filename);
return model;
}
@@ -46,8 +47,8 @@ namespace FamilyTreeAPI.Repository
int statuscode = 0;
string error = "";//Other
string photoType = "2"; //
if (criteria.PhotoType.HasValue)
photoType = criteria.PhotoType.Value.ToString();
if (criteria.PhotoType > 0)
photoType = criteria.PhotoType.ToString();
HttpContext? httpContext = _httpContext.HttpContext;
string loginName = "";
if (httpContext != null)
@@ -64,8 +65,8 @@ namespace FamilyTreeAPI.Repository
PersonPhoto model;
model = new();
model.PhotoType = photoType;
model = FillModel(model,criteria.PersonId, formfile.FileName);
model = FillModel(model,criteria.PersonId, criteria.PhotoType,formfile.FileName);
using (var stream = new MemoryStream())
{
@@ -93,14 +94,15 @@ namespace FamilyTreeAPI.Repository
};
}
public async Task<ResultModel<FileContent>> DownloadPersonPhoto(int id, string? filename)
public async Task<ResultModel<FileContent>> DownloadPersonPhoto(int personId, int photoType, string filename)
{
int statusCode = -1;
string error = "";
string extension = "";
PersonPhoto? model = null;
FileContent fileContent = new();
if (id < 1)
if (personId < 1)
{
var rlist = await _context.PersonPhotos.Where(x => x.Photo == filename).ToListAsync();
if (rlist != null)
@@ -111,19 +113,26 @@ namespace FamilyTreeAPI.Repository
}
}
}
else
{
model = await _context.PersonPhotos.FindAsync(id);
}
var rlist = await _context.PersonPhotos.Where (x => x.PersonId == personId && photoType.ToString() == x.PhotoType).ToListAsync();
for ( int i = 0; i < rlist.Count; i++)
{
model = rlist[i];
break;
}
}
if (model != null)
{
fileContent.ContentType = Helpers.GetFileContent(model.PhotoType);
extension = Helpers.GetFileExtension(model.Photo);
fileContent.ContentType = Helpers.GetFileContent(extension);
fileContent.Content = (byte[])model.ImagePhoto;
fileContent.FileName = model.Photo;
statusCode = 1;
}
return new ResultModel<FileContent>()
{
Data = fileContent,
@@ -132,20 +141,69 @@ namespace FamilyTreeAPI.Repository
};
}
public async Task<ResultModel<int>> DeletePersonPhoto(int id)
public async Task<ResultModel<int>> DeletePersonPhoto(int personId, int photoType)
{
int result = default(int);
int statuscode = 0;
string error = "";
PersonPhoto? model;
try
{
PersonPhoto? model = await _context.PersonPhotos.FindAsync(id);
if (model != null)
if (photoType < 1)
{
_context.PersonPhotos.Remove(model);
await _context.SaveChangesAsync();
statuscode = 1;
List<PersonPhoto> list = await _context.PersonPhotos.Where ( x => x.PersonId == personId).ToListAsync();
for (int i = 0; i< list.Count; i++)
{
model = list[i];
_context.PersonPhotos.Remove(model);
await _context.SaveChangesAsync();
statuscode = 1;
}
}
else
{
List<PersonPhoto> list = await _context.PersonPhotos.Where ( x => x.PersonId == personId && x.PhotoType == photoType.ToString()).ToListAsync();
for (int i = 0; i< list.Count; i++)
{
model = list[i];
_context.PersonPhotos.Remove(model);
await _context.SaveChangesAsync();
statuscode = 1;
}
}
}
catch (Exception ex)
{
error = ex.ToString();
statuscode = -1;
}
//var dto = await Task.Run(() => result);
return new ResultModel<int>()
{
Data = result,
StatusCode = statuscode,
Message = error
};
}
public async Task<ResultModel<int>> DeletePersonPhotoById(int id)
{
int result = default(int);
int statuscode = 0;
string error = "";
try
{
PersonPhoto? model = await _context.PersonPhotos.FindAsync(id);
if (model != null)
{
_context.PersonPhotos.Remove(model);
await _context.SaveChangesAsync();
statuscode = 1;
}
}
catch (Exception ex)
{