feat(model): refactor and add tags to Service model

This commit is contained in:
Julien Oculi 2024-06-17 18:00:28 +02:00
parent 42006cc18e
commit fda7b03e59

View file

@ -1,24 +1,22 @@
import { Ref } from '@/src/models/utils/ref.ts' 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 type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
export class Service extends Ressource { export class Service extends Ressource {
static fromJSON( static fromJSON(
{ groups, url, avatar, ...json }: ToJson<Service>, { groups, ...json }: ToJson<Service>,
): Service { ): Service {
return new Service({ return new Service({
groups: groups.map((group) => Ref.fromString<Group>(group)), groups: groups.map((group) => Ref.fromString<Group>(group)),
url: new URL(url),
avatar: new URL(avatar),
...json, ...json,
}) })
} }
static create( static create(
{ category, url, groups, avatar, name }: Pick< { category, tags, url, groups, avatar, name }: Pick<
Service, Service,
'category' | 'url' | 'groups' | 'avatar' | 'name' 'category' | 'tags' | 'url' | 'groups' | 'avatar' | 'name'
>, >,
): Service { ): Service {
const { uuid, createdAt, updatedAt } = super.create({ name }) const { uuid, createdAt, updatedAt } = super.create({ name })
@ -29,18 +27,21 @@ export class Service extends Ressource {
name, name,
category, category,
url, url,
tags,
groups, groups,
avatar, avatar,
}) })
} }
#category: 'bin' | 'web' | 'fs' | 'git' #category: 'web' | 'fs' | 'git'
#url: URL #url: UrlString
#groups: readonly Ref<Group>[] #groups: readonly Ref<Group>[]
#avatar: URL #avatar: UrlString
#tags: readonly string[]
private constructor({ private constructor({
category, category,
tags,
url, url,
groups, groups,
avatar, avatar,
@ -53,20 +54,22 @@ export class Service extends Ressource {
| 'name' | 'name'
| 'category' | 'category'
| 'url' | 'url'
| 'tags'
| 'groups' | 'groups'
| 'avatar' | 'avatar'
>) { >) {
super(ressource) super(ressource)
if (!['bin', 'web', 'fs', 'git'].includes(category)) { if (!['web', 'fs', 'git'].includes(category)) {
throw new TypeError( 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.#category = category
this.#url = url this.#url = new URL(url).href as UrlString
this.#groups = Object.freeze(groups) this.#groups = Object.freeze(groups)
this.#avatar = avatar this.#avatar = new URL(avatar).href as UrlString
this.#tags = Object.freeze(tags)
} }
get type(): 'service' { get type(): 'service' {
@ -77,16 +80,20 @@ export class Service extends Ressource {
return this.#category return this.#category
} }
get tags() {
return this.#tags
}
get url() { get url() {
return new URL(this.#url) return this.#url
} }
get groups() { get groups() {
return Object.freeze(this.#groups) return this.#groups
} }
get avatar() { get avatar() {
return new URL(this.#avatar) return this.#avatar
} }
update( update(
@ -102,9 +109,10 @@ export class Service extends Ressource {
...super.toJSON(), ...super.toJSON(),
type: this.type, type: this.type,
category: this.category, category: this.category,
url: this.url.toJSON(), tags: this.tags,
url: this.url,
groups: this.groups.map((group) => group.toJSON()), groups: this.groups.map((group) => group.toJSON()),
avatar: this.avatar.toJSON(), avatar: this.avatar,
} as const } as const
} }
@ -115,8 +123,9 @@ export class Service extends Ressource {
export interface Service extends Ressource { export interface Service extends Ressource {
type: 'service' type: 'service'
category: 'bin' | 'web' | 'fs' | 'git' category: 'web' | 'fs' | 'git'
url: URL tags: readonly string[]
url: UrlString
groups: readonly Ref<Group>[] groups: readonly Ref<Group>[]
avatar: URL avatar: UrlString
} }