Skip to content
Merged
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
2 changes: 2 additions & 0 deletions common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type {
Gender,
RelationshipStatus,
InterestSubject,
Interest,
User,
InitUser,
UpdateUser,
Expand All @@ -17,6 +18,7 @@ export type {
Course,
Enrollment,
Day,
UserWithCoursesAndSubjects,
MessageID,
ShareRoomID,
Message,
Expand Down
10 changes: 10 additions & 0 deletions common/zod/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export const InterestSubjectSchema = z.object({
group: z.string(),
});

export const InterestSchema = z.object({
userId: UserIDSchema,
subjectId: z.number(),
});

export const UserSchema = z.object({
id: UserIDSchema,
guid: GUIDSchema,
Expand Down Expand Up @@ -103,6 +108,11 @@ export const EnrollmentSchema = z.object({
courseId: CourseIDSchema,
});

export const UserWithCoursesAndSubjectsSchema = UserSchema.extend({
courses: CourseSchema.array(),
interestSubjects: InterestSubjectSchema.array(),
});

export const MessageIDSchema = z.number(); // TODO! Add __internal_prevent_cast_MessageID: PhantomData
export const ShareRoomIDSchema = z.number();

Expand Down
6 changes: 6 additions & 0 deletions common/zod/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
InitRoomSchema,
InitSharedRoomSchema,
InitUserSchema,
InterestSchema,
InterestSubjectSchema,
IntroLongSchema,
IntroShortSchema,
Expand All @@ -37,6 +38,7 @@ import type {
UpdateUserSchema,
UserIDSchema,
UserSchema,
UserWithCoursesAndSubjectsSchema,
} from "./schemas";

export type UserID = z.infer<typeof UserIDSchema>;
Expand All @@ -47,6 +49,7 @@ export type PictureUrl = z.infer<typeof PictureUrlSchema>;
export type Gender = z.infer<typeof GenderSchema>;
export type RelationshipStatus = z.infer<typeof RelationshipStatusSchema>;
export type InterestSubject = z.infer<typeof InterestSubjectSchema>;
export type Interest = z.infer<typeof InterestSchema>;
export type User = z.infer<typeof UserSchema>;
export type InitUser = z.infer<typeof InitUserSchema>;
export type UpdateUser = z.infer<typeof UpdateUserSchema>;
Expand All @@ -59,6 +62,9 @@ export type Course = z.infer<typeof CourseSchema>;
export type Enrollment = z.infer<typeof EnrollmentSchema>;
export type Day = z.infer<typeof DaySchema>;
export type Period = z.infer<typeof PeriodSchema>;
export type UserWithCoursesAndSubjects = z.infer<
typeof UserWithCoursesAndSubjectsSchema
>;
export type MessageID = z.infer<typeof MessageIDSchema>;
export type ShareRoomID = z.infer<typeof ShareRoomIDSchema>;
export type Message = z.infer<typeof MessageSchema>;
Expand Down
107 changes: 100 additions & 7 deletions server/src/database/requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Err, Ok, type Result } from "common/lib/result";
import type { Relationship, User, UserID } from "common/types";
import type {
Relationship,
UserID,
UserWithCoursesAndSubjects,
} from "common/types";
import { prisma } from "./client";

// マッチリクエストの送信
Expand Down Expand Up @@ -114,7 +118,7 @@ export async function cancelRequest(
//ユーザーへのリクエストを探す 俺をリクエストしているのは誰だ
export async function getPendingRequestsToUser(
userId: UserID,
): Promise<Result<User[]>> {
): Promise<Result<UserWithCoursesAndSubjects[]>> {
try {
const found = await prisma.user.findMany({
where: {
Expand All @@ -125,8 +129,37 @@ export async function getPendingRequestsToUser(
},
},
},
include: {
enrollments: {
include: {
course: {
include: {
enrollments: true,
slots: true,
},
},
},
},
interests: {
include: {
subject: true,
},
},
},
});
return Ok(found);
return Ok(
found.map((user) => {
return {
...user,
interestSubjects: user.interests.map((interest) => {
return interest.subject;
}),
courses: user.enrollments.map((enrollment) => {
return enrollment.course;
}),
};
}),
);
} catch (e) {
return Err(e);
}
Expand All @@ -135,7 +168,7 @@ export async function getPendingRequestsToUser(
//ユーザーがリクエストしている人を探す 俺がリクエストしているのは誰だ
export async function getPendingRequestsFromUser(
userId: UserID,
): Promise<Result<User[]>> {
): Promise<Result<UserWithCoursesAndSubjects[]>> {
try {
const found = await prisma.user.findMany({
where: {
Expand All @@ -146,15 +179,46 @@ export async function getPendingRequestsFromUser(
},
},
},
include: {
enrollments: {
include: {
course: {
include: {
enrollments: true,
slots: true,
},
},
},
},
interests: {
include: {
subject: true,
},
},
},
});
return Ok(found);
return Ok(
found.map((user) => {
return {
...user,
interestSubjects: user.interests.map((interest) => {
return interest.subject;
}),
courses: user.enrollments.map((enrollment) => {
return enrollment.course;
}),
};
}),
);
} catch (e) {
return Err(e);
}
}

//マッチした人の取得
export async function getMatchedUser(userId: UserID): Promise<Result<User[]>> {
export async function getMatchedUser(
userId: UserID,
): Promise<Result<UserWithCoursesAndSubjects[]>> {
try {
const found = await prisma.user.findMany({
where: {
Expand All @@ -177,8 +241,37 @@ export async function getMatchedUser(userId: UserID): Promise<Result<User[]>> {
},
],
},
include: {
enrollments: {
include: {
course: {
include: {
enrollments: true,
slots: true,
},
},
},
},
interests: {
include: {
subject: true,
},
},
},
});
return Ok(found);
return Ok(
found.map((user) => {
return {
...user,
interestSubjects: user.interests.map((interest) => {
return interest.subject;
}),
courses: user.enrollments.map((enrollment) => {
return enrollment.course;
}),
};
}),
);
} catch (e) {
return Err(e);
}
Expand Down
111 changes: 103 additions & 8 deletions server/src/database/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Err, Ok, type Result } from "common/lib/result";
import type { GUID, UpdateUser, User, UserID } from "common/types";
import type {
Course,
GUID,
InterestSubject,
UpdateUser,
User,
UserID,
UserWithCoursesAndSubjects,
} from "common/types";
import { prisma } from "./client";

// ユーザーの作成
Expand All @@ -17,15 +25,42 @@ export async function createUser(
}

// ユーザーの取得
export async function getUser(guid: GUID): Promise<Result<User>> {
export async function getUser(
guid: GUID,
): Promise<Result<UserWithCoursesAndSubjects>> {
try {
const user = await prisma.user.findUnique({
where: {
guid: guid,
},
include: {
enrollments: {
include: {
course: {
include: {
enrollments: true,
slots: true,
},
},
},
},
interests: {
include: {
subject: true,
},
},
},
});
if (!user) return Err(404);
return Ok(user);
return Ok({
...user,
interestSubjects: user.interests.map((interest) => {
return interest.subject;
}),
courses: user.enrollments.map((enrollment) => {
return enrollment.course;
}),
});
} catch (e) {
return Err(e);
}
Expand All @@ -50,14 +85,42 @@ export async function getUserIDByGUID(guid: GUID): Promise<Result<UserID>> {
.catch((err) => Err(err));
}

export async function getUserByID(id: UserID): Promise<Result<User>> {
export async function getUserByID(
id: UserID,
): Promise<Result<UserWithCoursesAndSubjects>> {
try {
const user = await prisma.user.findUnique({
where: {
id,
},
include: {
enrollments: {
include: {
course: {
include: {
enrollments: true,
slots: true,
},
},
},
},
interests: {
include: {
subject: true,
},
},
},
});
if (!user) return Err(404);
return Ok({
...user,
interestSubjects: user.interests.map((interest) => {
return interest.subject;
}),
courses: user.enrollments.map((enrollment) => {
return enrollment.course;
}),
});
return user === null ? Err(404) : Ok(user);
} catch (e) {
return Err(e);
}
Expand Down Expand Up @@ -100,10 +163,42 @@ export async function deleteUser(userId: UserID): Promise<Result<User>> {
}

// ユーザーの全取得
export async function getAllUsers(): Promise<Result<User[]>> {
export async function getAllUsers(): Promise<
Result<(User & { courses: Course[]; interestSubjects: InterestSubject[] })[]>
> {
try {
const users = await prisma.user.findMany();
return Ok(users);
const users = await prisma.user.findMany({
include: {
enrollments: {
include: {
course: {
include: {
enrollments: true,
slots: true,
},
},
},
},
interests: {
include: {
subject: true,
},
},
},
});
return Ok(
users.map((user) => {
return {
...user,
interestSubjects: user.interests.map((interest) => {
return interest.subject;
}),
courses: user.enrollments.map((enrollment) => {
return enrollment.course;
}),
};
}),
);
} catch (e) {
return Err(e);
}
Expand Down
Loading
Loading