Skip to content

Commit 162eee4

Browse files
committed
feat(shares): implement shares service
1 parent 88fcd23 commit 162eee4

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

src/shares/dto/share-info.dto.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Share, ShareType } from '../entities/share.entity';
2+
3+
export class ShareInfoDto {
4+
id: string;
5+
namespaceId: string;
6+
resourceId: string;
7+
enabled: boolean;
8+
requireLogin: boolean;
9+
passwordEnabled: boolean;
10+
shareType: ShareType;
11+
expiresAt: Date | null;
12+
13+
static fromEntity(share: Share | null): ShareInfoDto {
14+
if (!share) {
15+
return new ShareInfoDto();
16+
}
17+
const dto = new ShareInfoDto();
18+
dto.id = share.id;
19+
dto.namespaceId = share.namespaceId;
20+
dto.resourceId = share.resourceId;
21+
dto.enabled = share.enabled;
22+
dto.requireLogin = share.requireLogin;
23+
dto.passwordEnabled = !!share.password;
24+
dto.shareType = share.shareType;
25+
dto.expiresAt = share.expiresAt;
26+
return dto;
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ShareType } from '../entities/share.entity';
2+
3+
export class UpdateShareInfoReqDto {
4+
enabled?: boolean;
5+
requireLogin?: boolean;
6+
password?: string | null;
7+
shareType?: ShareType;
8+
expiresAt?: Date | null;
9+
}

src/shares/entities/share.entity.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class Share extends Base {
1515

1616
@BeforeInsert()
1717
generateId?() {
18-
this.id = generateId(16);
18+
this.id = generateId(6);
1919
}
2020

2121
@Column()
@@ -24,6 +24,9 @@ export class Share extends Base {
2424
@Column()
2525
resourceId: string;
2626

27+
@Column()
28+
enabled: boolean;
29+
2730
@Column()
2831
requireLogin: boolean;
2932

src/shares/shares.service.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Injectable } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
3-
import { Share } from './entities/share.entity';
3+
import { Share, ShareType } from './entities/share.entity';
44
import { Repository } from 'typeorm';
5+
import { ShareInfoDto } from './dto/share-info.dto';
6+
import { UpdateShareInfoReqDto } from './dto/update-share-info-req.dto';
57

68
@Injectable()
79
export class SharesService {
@@ -10,4 +12,57 @@ export class SharesService {
1012
private readonly shareRepo: Repository<Share>,
1113
) {}
1214

15+
async getShareInfo(
16+
namespaceId: string,
17+
resourceId: string,
18+
): Promise<ShareInfoDto> {
19+
const share = await this.shareRepo.findOne({
20+
where: {
21+
namespaceId,
22+
resourceId,
23+
},
24+
});
25+
return ShareInfoDto.fromEntity(share);
26+
}
27+
28+
async updateShareInfo(
29+
namespaceId: string,
30+
resourceId: string,
31+
req: UpdateShareInfoReqDto,
32+
): Promise<ShareInfoDto> {
33+
let share = await this.shareRepo.findOne({
34+
where: {
35+
namespaceId,
36+
resourceId,
37+
},
38+
});
39+
if (!share) {
40+
share = this.shareRepo.create({
41+
namespaceId,
42+
resourceId,
43+
enabled: false,
44+
requireLogin: true,
45+
shareType: ShareType.ALL,
46+
password: null,
47+
expiresAt: null,
48+
});
49+
}
50+
if (req.enabled !== undefined) {
51+
share.enabled = req.enabled;
52+
}
53+
if (req.requireLogin !== undefined) {
54+
share.requireLogin = req.requireLogin;
55+
}
56+
if (req.password !== undefined) {
57+
share.password = req.password;
58+
}
59+
if (req.shareType !== undefined) {
60+
share.shareType = req.shareType;
61+
}
62+
if (req.expiresAt !== undefined) {
63+
share.expiresAt = req.expiresAt;
64+
}
65+
const savedShare = await this.shareRepo.save(share);
66+
return ShareInfoDto.fromEntity(savedShare);
67+
}
1368
}

0 commit comments

Comments
 (0)