Skip to content

Commit

Permalink
feat(tutories): get cities and districts list
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Dec 1, 2024
1 parent 3c9ec44 commit c0f49e2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/module/tutor/tutor.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { tutors, tutories } from "@/db/schema";
import { eq, inArray } from "drizzle-orm/expressions";
import { db as dbType } from "@/db/config";
import { tutories, tutors } from "@/db/schema";
import { Tutor } from "@/types";
import { eq, inArray, isNotNull } from "drizzle-orm/expressions";

export class TutorRepository {
constructor(private readonly db: typeof dbType) {}
Expand All @@ -17,6 +17,24 @@ export class TutorRepository {
return await this.db.select().from(tutors);
}

public async getLocations() {
const [cities, districts] = await Promise.all([
this.db
.selectDistinct({ city: tutors.city })
.from(tutors)
.where(isNotNull(tutors.city)),
this.db
.selectDistinct({ district: tutors.district })
.from(tutors)
.where(isNotNull(tutors.district)),
]);

return {
cities: cities.map((c) => c.city),
districts: districts.map((d) => d.district),
};
}

public async hasTutors() {
const result = await this.db.select().from(tutors);
return result.length > 0;
Expand Down
13 changes: 13 additions & 0 deletions src/module/tutories/tutories.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ export const getAverageRate: Controller<GetAverageRateSchema> = async (
}
};

export const getLocations: Controller = async (_req, res) => {
try {
const locations = await tutoriesService.getLocations();
res.json({ status: "success", data: locations });
} catch (error) {
logger.error(`Failed to get all tutories location: ${error}`);
res.status(500).json({
status: "error",
message: `Failed to get all tutories location`,
});
}
};

export const getMyTutories: Controller = async (req, res) => {
const tutorId = req.tutor.id;

Expand Down
6 changes: 6 additions & 0 deletions src/module/tutories/tutories.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ tutoriesRouter.get(
tutoriesController.getAverageRate,
);

tutoriesRouter.get(
"/locations",
// #swagger.tags = ['tutors/services']
tutoriesController.getLocations,
);

tutoriesRouter.get(
"/:tutoriesId",
// #swagger.tags = ['tutors/services']
Expand Down
8 changes: 8 additions & 0 deletions src/module/tutories/tutories.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ export class TutoriesService {
}
}

async getLocations() {
try {
return await this.tutorRepository.getLocations();
} catch (error) {
logger.error(`Failed to get all tutories location: ${error}`);
}
}

async createTutories(
tutorId: string,
data: z.infer<typeof createTutoriesSchema>["body"],
Expand Down

0 comments on commit c0f49e2

Please sign in to comment.