feat(model): ✨ refactor and add tags to Service
model
This commit is contained in:
parent
42006cc18e
commit
fda7b03e59
|
@ -1,24 +1,22 @@
|
|||
import { Ref } from '@/src/models/utils/ref.ts'
|
||||
import type { ToJson } from '@/types.ts'
|
||||
import type { ToJson, UrlString } from '@/types.ts'
|
||||
import type { Group } from '@models/group.ts'
|
||||
import { Ressource } from '@models/ressource.ts'
|
||||
|
||||
export class Service extends Ressource {
|
||||
static fromJSON(
|
||||
{ groups, url, avatar, ...json }: ToJson<Service>,
|
||||
{ groups, ...json }: ToJson<Service>,
|
||||
): Service {
|
||||
return new Service({
|
||||
groups: groups.map((group) => Ref.fromString<Group>(group)),
|
||||
url: new URL(url),
|
||||
avatar: new URL(avatar),
|
||||
...json,
|
||||
})
|
||||
}
|
||||
|
||||
static create(
|
||||
{ category, url, groups, avatar, name }: Pick<
|
||||
{ category, tags, url, groups, avatar, name }: Pick<
|
||||
Service,
|
||||
'category' | 'url' | 'groups' | 'avatar' | 'name'
|
||||
'category' | 'tags' | 'url' | 'groups' | 'avatar' | 'name'
|
||||
>,
|
||||
): Service {
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name })
|
||||
|
@ -29,18 +27,21 @@ export class Service extends Ressource {
|
|||
name,
|
||||
category,
|
||||
url,
|
||||
tags,
|
||||
groups,
|
||||
avatar,
|
||||
})
|
||||
}
|
||||
|
||||
#category: 'bin' | 'web' | 'fs' | 'git'
|
||||
#url: URL
|
||||
#category: 'web' | 'fs' | 'git'
|
||||
#url: UrlString
|
||||
#groups: readonly Ref<Group>[]
|
||||
#avatar: URL
|
||||
#avatar: UrlString
|
||||
#tags: readonly string[]
|
||||
|
||||
private constructor({
|
||||
category,
|
||||
tags,
|
||||
url,
|
||||
groups,
|
||||
avatar,
|
||||
|
@ -53,20 +54,22 @@ export class Service extends Ressource {
|
|||
| 'name'
|
||||
| 'category'
|
||||
| 'url'
|
||||
| 'tags'
|
||||
| 'groups'
|
||||
| 'avatar'
|
||||
>) {
|
||||
super(ressource)
|
||||
|
||||
if (!['bin', 'web', 'fs', 'git'].includes(category)) {
|
||||
if (!['web', 'fs', 'git'].includes(category)) {
|
||||
throw new TypeError(
|
||||
`category is "${category}" but ('bin' | 'web' | 'fs' | 'git') is required`,
|
||||
`category is "${category}" but ('web' | 'fs' | 'git') is required`,
|
||||
)
|
||||
}
|
||||
this.#category = category
|
||||
this.#url = url
|
||||
this.#url = new URL(url).href as UrlString
|
||||
this.#groups = Object.freeze(groups)
|
||||
this.#avatar = avatar
|
||||
this.#avatar = new URL(avatar).href as UrlString
|
||||
this.#tags = Object.freeze(tags)
|
||||
}
|
||||
|
||||
get type(): 'service' {
|
||||
|
@ -77,16 +80,20 @@ export class Service extends Ressource {
|
|||
return this.#category
|
||||
}
|
||||
|
||||
get tags() {
|
||||
return this.#tags
|
||||
}
|
||||
|
||||
get url() {
|
||||
return new URL(this.#url)
|
||||
return this.#url
|
||||
}
|
||||
|
||||
get groups() {
|
||||
return Object.freeze(this.#groups)
|
||||
return this.#groups
|
||||
}
|
||||
|
||||
get avatar() {
|
||||
return new URL(this.#avatar)
|
||||
return this.#avatar
|
||||
}
|
||||
|
||||
update(
|
||||
|
@ -102,9 +109,10 @@ export class Service extends Ressource {
|
|||
...super.toJSON(),
|
||||
type: this.type,
|
||||
category: this.category,
|
||||
url: this.url.toJSON(),
|
||||
tags: this.tags,
|
||||
url: this.url,
|
||||
groups: this.groups.map((group) => group.toJSON()),
|
||||
avatar: this.avatar.toJSON(),
|
||||
avatar: this.avatar,
|
||||
} as const
|
||||
}
|
||||
|
||||
|
@ -115,8 +123,9 @@ export class Service extends Ressource {
|
|||
|
||||
export interface Service extends Ressource {
|
||||
type: 'service'
|
||||
category: 'bin' | 'web' | 'fs' | 'git'
|
||||
url: URL
|
||||
category: 'web' | 'fs' | 'git'
|
||||
tags: readonly string[]
|
||||
url: UrlString
|
||||
groups: readonly Ref<Group>[]
|
||||
avatar: URL
|
||||
avatar: UrlString
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue