Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add application service #19

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/DTO/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DTO

- 外部に公開できるように値オブジェクトのデータを加工する
9 changes: 9 additions & 0 deletions src/DTO/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class UserData {
readonly id: string;
readonly name: string;

constructor(id: string, name: string) {
this.id = id;
this.name = name;
}
}
6 changes: 3 additions & 3 deletions src/Domain/Entities/__tests__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { User, UserId } from "../user";

test("equal method", () => {
const userId1 = new UserId("hoge");
const user1 = new User(userId1, "user1");
const user1 = new User("user1", userId1.value);

const userId2 = new UserId("foo");
const user2 = new User(userId2, "user2");
const user2 = new User("user2", userId2.value);

// valid case
expect(user1.equals(user2)).toBeFalsy();

// no happen case (id is unique)
const user3 = new User(userId1, "user3");
const user3 = new User("user3", userId1.value);

expect(user1.equals(user3)).toBeTruthy();
expect(user2.equals(user3)).toBeFalsy();
Expand Down
10 changes: 7 additions & 3 deletions src/Domain/Entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@ export class UserId {

this.id = id;
}

public get value(): string {
return this.id;
}
}

export class User {
readonly id: UserId;
readonly id: string;
name: string;

constructor(id: UserId, name: string) {
constructor(name: string, id?: string, isNew = false) {
if (isNull(id) || isUndefined(id)) {
throw new Error("id requires UserId instance");
}
if (isNull(name) || isUndefined(name) || name === "") {
throw new Error("nameは1文字以上の文字列を期待しています");
}

this.id = id;
this.id = isNew ? new UserId("new").value : id;
this.name = this.changeName(name);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Repository/userRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { clone } from "../utils";

export interface IUserRepository {
save(user: User): void;
find(userId: UserId): null | User;
find(userName: string): null | User;
delete(user: User): void;
}

export class UserRepository implements IUserRepository {
Expand Down Expand Up @@ -50,4 +52,8 @@ export class InMemoryUserRepository implements IUserRepository {
return clone<User>(target);
}
}

delete(user: User): void {
// TODO: impl
}
}
35 changes: 35 additions & 0 deletions src/Service/Application/userApplicationService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { User } from "@/Domain/Entities/user";
import { UserData } from "@/DTO/user";
import { IUserRepository } from "@/Repository/userRepository";
import { UserService } from "../Domain/UserService";

export class UserApplicationService {
private readonly userRepository: IUserRepository;
private readonly userService: UserService;

constructor(repository: IUserRepository, service: UserService) {
this.userRepository = repository;
this.userService = service;
}

get(userId: string): UserData {
const user = this.userRepository.find(userId);

if (user === null) {
throw new Error("not exists User");
}

const userData = new UserData(user?.id, user?.name);
return userData;
}

register(name: string): void {
const user = new User(name);

if (this.userService.exist(user)) {
throw new Error("Already exists user");
}

this.userRepository.save(user);
}
}
2 changes: 1 addition & 1 deletion src/Service/Domain/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { User } from "@/Domain/Entities/user";
import { IUserRepository } from "@/Repository/userRepository";

export class UserService {
private userRepository: IUserRepository;
private readonly userRepository: IUserRepository;

constructor(userRepository: IUserRepository) {
this.userRepository = userRepository;
Expand Down
2 changes: 1 addition & 1 deletion src/UseCase/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Program {

createUser(userName: string): void {
const id = new UserId("id");
const user = new User(id, userName);
const user = new User(userName, id);
const userService = new UserService(this.userRepository);

if (userService.exist(user)) {
Expand Down