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

Migrated to typescript #7

Merged
merged 1 commit into from
Jan 14, 2025
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
20 changes: 0 additions & 20 deletions app/(api)/_datalib/_resolvers/Playlist.js

This file was deleted.

23 changes: 23 additions & 0 deletions app/(api)/_datalib/_resolvers/Playlist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Playlists from '../_services/Playlists';
import Users from '../_services/Users';
import { Playlist, PlaylistInput } from '@datatypes/Playlist';

const resolvers = {
Playlist: {
user: (parent: Playlist) => Users.find(parent.userId),
songs: (parent: Playlist) => Playlists.getSongs(parent.id),
},
Query: {
playlist: (_: never, args: { id: string }) => Playlists.find(args.id),
playlists: () => Playlists.findAll(),
},
Mutation: {
createPlaylist: (
_: never,
args: { userId: string; input: PlaylistInput }
) => Playlists.create(args.userId, args.input),
addSong: (_: never, args: { playlistId: string; songId: string }) =>
Playlists.addSong(args.playlistId, args.songId),
},
};
export default resolvers;
15 changes: 0 additions & 15 deletions app/(api)/_datalib/_resolvers/Song.js

This file was deleted.

17 changes: 17 additions & 0 deletions app/(api)/_datalib/_resolvers/Song.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Songs from '../_services/Songs';
import { SongInput } from '@datatypes/Song';

const resolvers = {
Song: {
playlists: () => null,
},
Query: {
song: (_: never, args: { id: string }) => Songs.find(args.id),
songs: () => Songs.findAll(),
},
Mutation: {
createSong: (_: never, args: { input: SongInput }) =>
Songs.create(args.input),
},
};
export default resolvers;
18 changes: 0 additions & 18 deletions app/(api)/_datalib/_resolvers/User.js

This file was deleted.

21 changes: 21 additions & 0 deletions app/(api)/_datalib/_resolvers/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Users from '../_services/Users';
import { User, UserInput } from '@datatypes/User';

const resolvers = {
User: {
playlists: (parent: User) => Users.getPlaylists(parent.id),
},
Query: {
user: (_: never, args: { id: string }) => Users.find(args.id),
users: (_: never, args: { ids: string[] }) => Users.findMany(args.ids),
},
Mutation: {
createUser: (_: never, args: { input: UserInput }) =>
Users.create(args.input),
updateUser: (_: never, args: { id: string; input: UserInput }) =>
Users.update(args.id, args.input),
deleteUser: (_: never, args: { id: string }) => Users.delete(args.id),
},
};

export default resolvers;
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { mergeResolvers } from '@graphql-tools/merge';
import type { IResolvers } from '@graphql-tools/utils';

import User from './User.js';
import Playlist from './Playlist.js';
import Song from './Song.js';
import User from './User';
import Playlist from './Playlist';
import Song from './Song';

const allResolvers = [];
const allResolvers: IResolvers[] = [];

const modules = [User, Playlist, Song];
modules.forEach((module) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import prisma from '../_prisma/client.js';
import prisma from '../_prisma/client';
import { PlaylistInput } from '@datatypes/Playlist';

export default class Playlists {
// CREATE
static async create({ userId, input }) {
static async create(userId: string, input: PlaylistInput) {
const { name } = input;
const playlist = await prisma.playlist.create({
data: {
Expand All @@ -14,7 +15,7 @@ export default class Playlists {
}

// READ
static async find({ id }) {
static async find(id: string) {
return prisma.playlist.findUnique({ where: { id } });
}

Expand All @@ -23,7 +24,7 @@ export default class Playlists {
}

// OTHER
static async addSong({ playlistId, songId }) {
static async addSong(playlistId: string, songId: string) {
try {
await prisma.playlistToSong.create({
data: {
Expand All @@ -37,7 +38,7 @@ export default class Playlists {
}
}

static async getSongs({ id }) {
static async getSongs(id: string) {
const songIds = await prisma.playlistToSong.findMany({
where: {
playlistId: id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import prisma from '../_prisma/client.js';
import prisma from '../_prisma/client';
import { SongInput } from '@datatypes/Song';

export default class Songs {
// CREATE
static async create({ input }) {
static async create(input: SongInput) {
const { name } = input;
const song = await prisma.song.create({
data: {
Expand All @@ -13,7 +14,7 @@ export default class Songs {
}

// READ
static async find({ id }) {
static async find(id: string) {
return prisma.song.findUnique({ where: { id } });
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import prisma from '../_prisma/client.js';
import { UserInput } from '@datatypes/User';
import prisma from '../_prisma/client';

export default class Users {
// CREATE
static async create({ input }) {
static async create(input: UserInput) {
const { name } = input;
const user = await prisma.user.create({
data: {
Expand All @@ -13,15 +14,16 @@ export default class Users {
}

// READ
static async find({ id }) {
static async find(id: string) {
return prisma.user.findUnique({
where: {
id,
},
});
}

static async findMany({ ids }) {
static async findMany(ids: string[]) {
if (ids.length === 0) return prisma.user.findMany();
return prisma.user.findMany({
where: {
id: {
Expand All @@ -32,7 +34,7 @@ export default class Users {
}

// UDPATE
static async update({ id, input }) {
static async update(id: string, input: UserInput) {
try {
const user = await prisma.user.update({
where: {
Expand All @@ -47,7 +49,7 @@ export default class Users {
}

// DELETE
static async delete({ id }) {
static async delete(id: string) {
try {
await prisma.user.delete({
where: {
Expand All @@ -61,10 +63,10 @@ export default class Users {
}

// OTHER
static async getPlaylists({ id }) {
static async getPlaylists(userId: string) {
return prisma.playlist.findMany({
where: {
userId: id,
userId,
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { mergeTypeDefs } from '@graphql-tools/merge';
import type { DocumentNode } from 'graphql';

import User from './User.js';
import Playlist from './Playlist.js';
import Song from './Song.js';
import User from './User';
import Playlist from './Playlist';
import Song from './Song';

const allTypeDefs = [];
const allTypeDefs: DocumentNode[] = [];

const modules = [User, Playlist, Song];
modules.forEach((module) => {
Expand Down
15 changes: 15 additions & 0 deletions app/(api)/_types/Playlist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { User } from '@datatypes/User';
import { Song } from '@datatypes/Song';

export type Playlist = {
id: string;
name: string;
user: User;
userId: string;
songs: Song[];
};

export type PlaylistInput = {
name: string;
userId: string;
};
11 changes: 11 additions & 0 deletions app/(api)/_types/Song.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Playlist } from '@datatypes/Playlist';

export type Song = {
id: string;
name: string;
playlists: Playlist[];
};

export type SongInput = {
name: string;
};
11 changes: 11 additions & 0 deletions app/(api)/_types/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Playlist } from '@datatypes/Playlist';

export type User = {
id: string;
name: string;
playlists: Playlist[];
};

export type UserInput = {
name: string;
};
27 changes: 17 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@utils/*": ["./app/(pages)/_utils/*"],
"@actions/*": ["./app/(api)/_actions/*"],
"@datalib/*": ["./app/(api)/_datalib/*"],
"@datatypes/*": ["./app/(api)/_types/*"],
}
},
"include": ["nextEnv.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
Expand Down