Skip to content

Commit

Permalink
[Back | Read] UserEntity & UserGetList & UserGetMe - [Front | Comp] O…
Browse files Browse the repository at this point in the history
…rderPersona - [Front | Hub] OrderUser (#55)

* UserEntity

* OrderUser

* 1

* UserGetList

* UserGetMe

* 1
  • Loading branch information
Aloento authored Oct 28, 2023
1 parent 8fc5f2b commit 3fa1eda
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 87 deletions.
7 changes: 7 additions & 0 deletions TSystems.LoveOTC/AdminHub/User/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ internal partial class AdminHub {

return await this.Db.Users
.Where(x => x.UserId == key)
.Select(x => new {
x.Name,
x.EMail,
x.Phone,
x.Address,
x.Admin
})
.SingleOrDefaultAsync();
}
}
40 changes: 6 additions & 34 deletions TSystems.LoveOTC/AdminHub/User/Get.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
namespace TSystems.LoveOTC.AdminHub;

using Hub;
using Microsoft.EntityFrameworkCore;

internal partial class AdminHub {
/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 1.0.0
* </remarks>
*/
public async Task<Persona> UserGetOrderUser(string orderId) {
return new() {
Name = "Aloento",
Phone = "+36 300000000",
EMail = "[email protected]",
Address = string.Concat(Enumerable.Range(0, 10).Select(_ => Guid.NewGuid().ToString()))
};
}

/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* </remarks>
*/
public async Task<List<UserItem>> UserGetList() {
return new()
{
new() {
Id = Guid.NewGuid(),
Name = "Aloento",
EMail = "[email protected]",
Admin = true
},
new() {
Id = Guid.NewGuid(),
Name = "SomeOne",
EMail = "[email protected]"
}
};
}
public async Task<Guid[]> UserGetList() =>
await this.Db.Users
.Select(x => x.UserId)
.ToArrayAsync();
}
29 changes: 18 additions & 11 deletions TSystems.LoveOTC/Hub/User/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@ internal partial class ShopHub {
* </remarks>
*/
[Authorize]
public async Task<Persona> UserGetMe() {
public async Task<dynamic?> UserGetMe(byte _, uint? version) {
var hasNew = this.Context.Items.TryGetValue("NewUser", out var isNew);
if (hasNew && isNew is true) return null!;
if (hasNew && isNew is true) return null;

var res = await this.Db.Users
.Where(x => x.UserId == this.UserId)
.Select(user => new Persona {
Name = user.Name,
EMail = user.EMail,
Phone = user.Phone,
Address = user.Address
}).FirstAsync();
if (version is not null) {
var noChange = await this.Db.Users
.AnyAsync(x => x.UserId == this.UserId && x.Version == version);

if (noChange) return true;
}

return res;
return await this.Db.Users
.Where(x => x.UserId == this.UserId)
.Select(x => new {
x.Name,
x.EMail,
x.Phone,
x.Address,
x.Version
})
.SingleOrDefaultAsync();
}
}
14 changes: 11 additions & 3 deletions src/Components/Persona.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,26 @@ const useStyles = makeStyles({
/**
* @author Aloento
* @since 0.5.0
* @version 0.3.0
* @version 0.4.0
*/
export function OrderPersona({ OrderId, Admin }: { OrderId: number; Admin?: true }) {
const style = useStyles();
const { data } = useRequest(Admin ? AdminHub.User.Get.OrderUser.bind(AdminHub.User.Get) : Hub.User.Get.Me.bind(Hub.User.Get), {
defaultParams: [OrderId]

const { data: admin } = useRequest(AdminHub.User.Get.OrderUser.bind(AdminHub.User.Get), {
defaultParams: [OrderId],
manual: !Admin
})

const { data: me } = useRequest(Hub.User.Get.Me.bind(Hub.User.Get), {
manual: Admin
});

const { data: order } = useRequest(Hub.Order.Get.Order.bind(Hub.Order.Get), {
defaultParams: [OrderId]
});

const data = Admin ? admin : me;

return <>
<div className={style.flex}>
<div className={style.box}>
Expand Down
5 changes: 4 additions & 1 deletion src/Components/Setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export function Setting({ Open, Toggle, New }: ISetting) {

useRequest(Hub.User.Get.Me.bind(Hub.User.Get), {
manual: New,
onSuccess({ Name, Address, Phone }) {
onSuccess(data) {
if (!data) return;
const { Name, Phone, Address } = data;

setName(Name);
setPhone(Phone);
setAddress(Address);
Expand Down
2 changes: 1 addition & 1 deletion src/Pages/Admin/User/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AdminHub } from "~/ShopNet/Admin";
* @since 0.5.0
* @version 0.1.0
*/
export function AdminUserAdmin({ UserId, Admin, Refresh }: { UserId: string; Admin?: true; Refresh: () => void }) {
export function AdminUserAdmin({ UserId, Admin, Refresh }: { UserId: string; Admin?: boolean; Refresh: () => void }) {
const { dispatchError, dispatchToast } = use500Toast();

const { run: grant } = useRequest(AdminHub.User.Post.Admin.bind(AdminHub.User.Post), {
Expand Down
2 changes: 1 addition & 1 deletion src/Pages/Admin/User/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface IUserItem {
Id: string;
Name: string;
EMail: string;
Admin?: true;
Admin?: boolean;
}

/**
Expand Down
42 changes: 35 additions & 7 deletions src/ShopNet/Admin/User/Get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import dayjs from "dayjs";
import { IPersona } from "~/Components/ShopCart/Persona";
import { IUserItem } from "~/Pages/Admin/User";
import { AdminNet } from "../AdminNet";
import { AdminOrderEntity } from "../Order/Entity";
import { AdminUserEntity } from "./Entity";

/**
* @author Aloento
Expand All @@ -11,22 +14,47 @@ export class AdminUserGet extends AdminNet {
/**
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 1.0.0
*/
public static async OrderUser(orderId: number): Promise<IPersona> {
await this.EnsureConnected();
const res = await this.Hub.invoke<IPersona>("UserGetOrderUser", orderId);
return res;
const order = await AdminOrderEntity.Order(orderId);

if (!order)
throw new Error(`Order ${orderId} not found`);

const user = await AdminUserEntity.User(order.UserId);

if (!user)
throw new Error(`User ${order.UserId} not found in order ${orderId}`);

return user;
}

/**
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 1.0.0
*/
public static async List(): Promise<IUserItem[]> {
await this.EnsureConnected();
const res = await this.Hub.invoke<IUserItem[]>("UserGetList");
const list = await this.WithTimeCache<string[]>("", "UserGetList", dayjs().add(1, "m"));
const res: IUserItem[] = [];

for (const userId of list) {
const user = await AdminUserEntity.User(userId);

if (!user) {
console.warn(`User ${userId} not found`);
continue;
}

res.push({
Id: userId,
Name: user.Name,
EMail: user.EMail,
Admin: user.Admin,
});
}

return res;
}
}
2 changes: 1 addition & 1 deletion src/ShopNet/Admin/User/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class AdminUserPost extends AdminNet {
* @since 0.5.0
* @version 0.1.0
*/
public static async Admin(userId: number): Promise<true> {
public static async Admin(userId: string): Promise<true> {
await this.EnsureConnected();
const res = await this.Hub.invoke<true>("UserPostAdmin", userId);
return res;
Expand Down
22 changes: 1 addition & 21 deletions src/ShopNet/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import dayjs, { Dayjs } from "dayjs";
import Dexie from "dexie";

/**
* 缓存实体
*
* @author Aloento
* @since 0.3.1 MusiLand
* @version 0.2.1
Expand All @@ -15,8 +13,6 @@ interface ITable<T> {
}

/**
* 数据表操作工具
*
* @author Aloento
* @since 0.3.1 MusiLand
* @version 0.2.1
Expand All @@ -30,12 +26,9 @@ export class Table<TPre = any> {
}

/**
* 按完整 key 获取
*
* @author Aloento
* @since 0.1.0 MusiLand
* @version 0.2.0
* @param expire 对象失效规则
*/
public async Get<T extends TPre = TPre>(key: string, expire?: (x?: ITable<T>) => Promise<boolean>): Promise<T | null> {
const find = await this.Sto.get(key) as ITable<T> | undefined;
Expand All @@ -54,14 +47,9 @@ export class Table<TPre = any> {
}

/**
* 获取或者创建
*
* @author Aloento
* @since 0.1.0 MusiLand
* @version 0.2.0
* @param fac 内容生成工厂
* @param exp 过期时间
* @returns 工厂创建的结果
*/
public async GetOrSet<T extends TPre = TPre>(
key: string,
Expand All @@ -75,15 +63,9 @@ export class Table<TPre = any> {
}

/**
* 存储,已存在时会替换
* 默认有一月的过期时间,但也可以设置为 null
* 高并发时不能保证 key 唯一
*
* @author Aloento
* @since 0.1.0 MusiLand
* @version 0.2.0
* @param exp 过期Token
* @returns val
*/
public async Set<T extends TPre = TPre>(id: string, val: T, exp?: Dayjs | null): Promise<T> {
if (!val)
Expand All @@ -98,7 +80,7 @@ export class Table<TPre = any> {
return val;
}

const time = (exp || dayjs().add(1, "M")).unix();
const time = (exp || dayjs().add(1, "week")).unix();
if (exp && time < dayjs().unix())
throw "The expiration time cannot be less than the current time";

Expand All @@ -111,8 +93,6 @@ export class Table<TPre = any> {
}

/**
* 清理过期的缓存
*
* @author Aloento
* @since 0.3.0 MusiLand
* @version 0.2.0
Expand Down
16 changes: 9 additions & 7 deletions src/ShopNet/User/Get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPersona } from "~/Components/ShopCart/Persona";
import { IConcurrency } from "../Database";
import { ShopNet } from "../ShopNet";

/**
Expand All @@ -9,14 +9,16 @@ import { ShopNet } from "../ShopNet";
export class UserGet extends ShopNet {
/**
* @author Aloento
* @since 0.5.0
* @since 1.0.0
* @version 0.1.0
*/
public static async Me(): Promise<IPersona> {
public static Me(): Promise<({
Name: string;
EMail: string;
Phone: string;
Address: string;
} & IConcurrency) | void> {
this.EnsureLogin();
await this.EnsureConnected();

const res = await this.Hub.invoke<IPersona>("UserGetMe");
return res;
return this.WithVersionCache(0, "UserGetMe");
}
}

0 comments on commit 3fa1eda

Please sign in to comment.