Switch DELETE response from 204 to 200 to meet client data expectations. Specify NextApiResponse returned data type in all api routes.

This commit is contained in:
Hikmet 2024-02-28 23:54:50 +03:00
parent 837aeb2b1c
commit cea29cf08f
11 changed files with 130 additions and 74 deletions

View File

@ -1,4 +1,3 @@
import ILesson from "../lib/lesson/lesson";
import ITheme from "../lib/theme/theme";
import { ApiResponse } from "./api_response";
import { BackendThemeService } from "../backend/services/theme_service";

View File

@ -5,7 +5,11 @@ import { ApiResponse } from "@/api/api_response";
export class BackendActivityService {
constructor(private readonly activityRepo: IActivityRepository) {}
async createActivity(themeId: string, lessonId: string, activity: IActivity) {
async createActivity(
themeId: string,
lessonId: string,
activity: IActivity
): Promise<ApiResponse> {
try {
const activityToCreate = { ...activity } as any;
delete activityToCreate.id;
@ -25,7 +29,11 @@ export class BackendActivityService {
}
}
async saveActivity(themeId: string, lessonId: string, activity: IActivity) {
async saveActivity(
themeId: string,
lessonId: string,
activity: IActivity
): Promise<ApiResponse> {
try {
const activityToSave = { ...activity } as any;
delete activityToSave.id;
@ -42,7 +50,11 @@ export class BackendActivityService {
}
}
async deleteActivity(themeId: string, lessonId: string, activityId: string) {
async deleteActivity(
themeId: string,
lessonId: string,
activityId: string
): Promise<ApiResponse> {
try {
await this.activityRepo.deleteActivity(themeId, lessonId, activityId);
return { status: "success", message: "Aktivite başarıyla silindi." };

View File

@ -1,11 +1,15 @@
import ILessonRepository from "@/backend/repositories/lesson/lesson_repository";
import ILesson from "../../lib/lesson/lesson";
import { convertLessonUserToDB } from "../utils/db_converters";
import { ApiResponse } from "@/api/api_response";
export class BackendLessonService {
constructor(private readonly lessonRepo: ILessonRepository) {}
createLesson = async (themeId: string, lesson: ILesson) => {
createLesson = async (
themeId: string,
lesson: ILesson
): Promise<ApiResponse> => {
try {
await this.lessonRepo.createLesson(
themeId,
@ -22,7 +26,10 @@ export class BackendLessonService {
}
};
saveLesson = async (themeId: string, lesson: Omit<ILesson, "activities">) => {
saveLesson = async (
themeId: string,
lesson: Omit<ILesson, "activities">
): Promise<ApiResponse> => {
try {
const lessonToSave = { ...lesson } as any;
delete lessonToSave.id;
@ -34,7 +41,10 @@ export class BackendLessonService {
}
};
deleteLesson = async (themeId: string, lessonId: string) => {
deleteLesson = async (
themeId: string,
lessonId: string
): Promise<ApiResponse> => {
try {
await this.lessonRepo.deleteLesson(themeId, lessonId);
return { status: "success", message: "Ders başarıyla silindi." };

View File

@ -51,7 +51,7 @@ export class BackendThemeService {
}
};
deleteTheme = async (themeId: string) => {
deleteTheme = async (themeId: string): Promise<ApiResponse> => {
try {
await this.themeRepository.deleteTheme(themeId);
return { status: "success", message: "Tema başarıyla silindi." };
@ -77,7 +77,7 @@ export class BackendThemeService {
}
};
createTheme = async (theme: ITheme) => {
createTheme = async (theme: ITheme): Promise<ApiResponse> => {
try {
const themeToCreate = { ...theme, pk: "theme" } as any;
await this.themeRepository.createTheme(
@ -95,7 +95,7 @@ export class BackendThemeService {
saveTheme = async (
theme: Pick<ITheme, "id" | "explanation" | "image" | "youtubeVideoUrl">
) => {
): Promise<ApiResponse> => {
try {
await this.themeRepository.saveTheme(theme);
return {

View File

@ -1,10 +1,11 @@
import { NextApiRequest, NextApiResponse } from "next";
import { SignJWT } from "jose";
import { setCookie } from "cookies-next";
import { ApiResponse } from "@/api/api_response";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
res: NextApiResponse<ApiResponse>
) {
if (req.method === "POST") {
const jwtSecretKey = process.env.JWT_SECRET_KEY;
@ -13,13 +14,18 @@ export default async function handler(
if (!jwtSecretKey) {
console.error("JWT_SECRET_KEY is not set in the environment variables");
return res.status(500).json({
error: "Internal server error. Please contact the administrator.",
});
return res
.status(500)
.json({
status: "error",
message: "Internal server error. Please contact the administrator.",
});
}
if (!password) {
return res.status(400).json({ error: "Password is required" });
return res
.status(400)
.json({ status: "error", message: "Password is required" });
}
if (password === jwtSecretKey) {
@ -31,11 +37,13 @@ export default async function handler(
setCookie("token", jwt, { req, res, maxAge: 60 * 60 * 24 * 360 });
return res.status(200).json({ message: "Logged in" });
return res.status(200).json({ status: "success", message: "Logged in" });
}
return res.status(401).json({ error: "Unauthorized" });
return res.status(401).json({ status: "error", message: "Unauthorized" });
}
return res.status(405).json({ error: "Unsopported request method" });
return res
.status(405)
.json({ status: "error", message: "Unsopported request method" });
}

View File

@ -1,34 +1,40 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { DynamoDBThemeRepository } from "@/backend/repositories/theme/dynamodb_theme_repository";
import { BackendThemeService } from "@/backend/services/theme_service";
import { ApiResponse } from "@/api/api_response";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
res: NextApiResponse<ApiResponse>
) {
const themeRepo = new DynamoDBThemeRepository();
const themeApiService = new BackendThemeService(themeRepo);
const backendThemeService = new BackendThemeService(themeRepo);
const theme = req.query.theme as string;
if (req.method === "GET") {
const repRes = await themeApiService.getTheme(theme);
return res.status(200).json(repRes);
const repRes = await backendThemeService.getTheme(theme);
res.status(200).json(repRes);
} else if (req.method === "PUT") {
const type = req.query.type;
if (type === "save-theme") {
const repRes = await themeApiService.saveTheme(req.body.theme);
return res.status(200).send(repRes);
const repRes = await backendThemeService.saveTheme(req.body.theme);
res.status(200).json(repRes);
} else if (type === "relocate-theme") {
const repRes = await themeApiService.relocateTheme(theme, req.body.theme);
return res.status(200).send(repRes);
const repRes = await backendThemeService.relocateTheme(
theme,
req.body.theme
);
res.status(200).json(repRes);
} else {
res.status(400).json({ status: "error", message: "Unsopported action" });
}
return res.status(400).json({ error: "Unsopported action" });
} else if (req.method === "DELETE") {
const repRes = await themeApiService.deleteTheme(theme);
return res.status(204).json(repRes);
const repRes = await backendThemeService.deleteTheme(theme);
res.status(200).json(repRes);
} else {
res
.status(405)
.json({ status: "error", message: "Unsopported request method" });
}
return res.status(405).json({ error: "Unsopported request method" });
}

View File

@ -1,13 +1,14 @@
import { ApiResponse } from "@/api/api_response";
import { DynamoDBActivityRepository } from "@/backend/repositories/activity/dynamo_db_activity_repository";
import { BackendActivityService } from "@/backend/services/activity_service";
import type { NextApiRequest, NextApiResponse } from "next";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
res: NextApiResponse<ApiResponse>
) {
const activityRepo = new DynamoDBActivityRepository();
const activityApiService = new BackendActivityService(activityRepo);
const backendActivityService = new BackendActivityService(activityRepo);
const { theme, lesson, activity } = req.query as {
theme: string;
lesson: string;
@ -15,27 +16,29 @@ export default async function handler(
};
if (req.method === "GET") {
const rawData = await activityApiService.getActivity(
const rawData = await backendActivityService.getActivity(
theme,
lesson,
activity
);
return res.status(200).json(rawData);
res.status(200).json(rawData);
} else if (req.method === "PUT") {
const repRes = await activityApiService.saveActivity(
const repRes = await backendActivityService.saveActivity(
theme,
lesson,
req.body.activity
);
return res.status(200).send(repRes);
res.status(200).json(repRes);
} else if (req.method === "DELETE") {
const repRes = await activityApiService.deleteActivity(
const repRes = await backendActivityService.deleteActivity(
theme,
lesson,
activity
);
return res.status(204).send(repRes);
res.status(200).json(repRes);
} else {
res
.status(405)
.json({ status: "error", message: "Unsopported request method" });
}
return res.status(405).json({ error: "Unsopported request method" });
}

View File

@ -1,23 +1,26 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { DynamoDBActivityRepository } from "@/backend/repositories/activity/dynamo_db_activity_repository";
import { BackendActivityService } from "@/backend/services/activity_service";
import { ApiResponse } from "@/api/api_response";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
res: NextApiResponse<ApiResponse>
) {
const activityRepo = new DynamoDBActivityRepository();
const activityApiService = new BackendActivityService(activityRepo);
const backendActivityService = new BackendActivityService(activityRepo);
const { theme, lesson } = req.query as { theme: string; lesson: string };
if (req.method === "POST") {
const repRes = await activityApiService.createActivity(
const repRes = await backendActivityService.createActivity(
theme,
lesson,
req.body.activity
);
return res.status(201).send(repRes);
res.status(201).json(repRes);
} else {
res
.status(405)
.json({ status: "error", message: "Unsopported request method" });
}
return res.status(405).json({ error: "Unsopported request method" });
}

View File

@ -1,22 +1,28 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { DynamoDBLessonRepository } from "@/backend/repositories/lesson/dynamodb_lesson_repository";
import { BackendLessonService } from "@/backend/services/lesson_service";
import { ApiResponse } from "@/api/api_response";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
res: NextApiResponse<ApiResponse>
) {
const lessonRepo = new DynamoDBLessonRepository();
const lessonApiService = new BackendLessonService(lessonRepo);
const backendLessonService = new BackendLessonService(lessonRepo);
const { theme, lesson } = req.query as { theme: string; lesson: string };
if (req.method === "PUT") {
const repRes = await lessonApiService.saveLesson(theme, req.body.lesson);
return res.status(200).send(repRes);
const repRes = await backendLessonService.saveLesson(
theme,
req.body.lesson
);
res.status(200).json(repRes);
} else if (req.method === "DELETE") {
const repRes = await lessonApiService.deleteLesson(theme, lesson);
return res.status(204).send(repRes);
const repRes = await backendLessonService.deleteLesson(theme, lesson);
res.status(200).json(repRes);
} else {
res
.status(405)
.json({ status: "error", message: "Unsopported request method" });
}
return res.status(405).json({ error: "Unsopported request method" });
}

View File

@ -1,19 +1,25 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { DynamoDBLessonRepository } from "@/backend/repositories/lesson/dynamodb_lesson_repository";
import { BackendLessonService } from "@/backend/services/lesson_service";
import { ApiResponse } from "@/api/api_response";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
res: NextApiResponse<ApiResponse>
) {
const lessonRepo = new DynamoDBLessonRepository();
const lessonApiService = new BackendLessonService(lessonRepo);
const backendLessonService = new BackendLessonService(lessonRepo);
const { theme } = req.query as { theme: string; lesson: string };
if (req.method === "POST") {
const repRes = await lessonApiService.createLesson(theme, req.body.lesson);
return res.status(201).send(repRes);
const repRes = await backendLessonService.createLesson(
theme,
req.body.lesson
);
res.status(201).json(repRes);
} else {
res
.status(405)
.json({ status: "error", message: "Unsopported request method" });
}
return res.status(405).json({ error: "Unsopported request method" });
}

View File

@ -1,30 +1,33 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { DynamoDBThemeRepository } from "@/backend/repositories/theme/dynamodb_theme_repository";
import { BackendThemeService } from "@/backend/services/theme_service";
import { ApiResponse } from "@/api/api_response";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
res: NextApiResponse<ApiResponse>
) {
const themeRepo = new DynamoDBThemeRepository();
const themeApiService = new BackendThemeService(themeRepo);
const backendThemeService = new BackendThemeService(themeRepo);
if (req.method === "POST") {
const repRes = await themeApiService.createTheme(req.body.theme);
return res.status(201).send(repRes);
const repRes = await backendThemeService.createTheme(req.body.theme);
res.status(201).json(repRes);
} else if (req.method === "GET") {
const type = req.query.type;
if (type === "path-names") {
const repRes = await themeApiService.getThemeIds();
return res.status(200).json(repRes);
const repRes = await backendThemeService.getThemeIds();
res.status(200).json(repRes);
} else if (type === "theme-metas") {
const repRes = await themeApiService.getThemeMetas();
return res.status(200).json(repRes);
const repRes = await backendThemeService.getThemeMetas();
res.status(200).json(repRes);
} else {
res.status(400).json({ status: "error", message: "Unsopported action" });
}
return res.status(400).json({ error: "Unsopported action" });
} else {
res
.status(405)
.json({ status: "error", message: "Unsopported request method" });
}
return res.status(405).json({ error: "Unsopported request method" });
}