Skip to content

Commit

Permalink
Merge pull request #3 from TarikNasraoui/feat/planets-info-in-astronauts
Browse files Browse the repository at this point in the history
Associate planet information with astronauts
  • Loading branch information
TarikNasraoui authored Sep 28, 2024
2 parents f342cb3 + d3eef10 commit a0ab542
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
21 changes: 16 additions & 5 deletions backend/src/controllers/AstronautController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import knex from '../db';
const AstronautController = {
getAll: async (req: Request, res: Response): Promise<void> => {
try {
const astronauts = (await knex('astronauts').select('astronauts.*', 'planets.name', 'planets.description', 'planets.isHabitable', 'images.path', 'images.name as imageName'))
.map(({ id, firstname, lastname, name, isHabitable, description, path, imageName }) => ({
const astronauts = (
await knex('astronauts')
.select('astronauts.*', 'planets.*', 'images.path', 'images.name as imageName')
.join('planets', 'planets.id', '=', 'astronauts.originPlanetId')
.join('images', 'images.id', '=', 'planets.imageId')
).map(({ id, firstname, lastname, name, isHabitable, description, path, imageName }) => ({
id,
firstname,
lastname,
Expand All @@ -28,8 +32,12 @@ const AstronautController = {
getById: async (req: Request, res: Response): Promise<void> => {
const { id } = req.params;
try {
const data = await knex('astronauts').select('astronauts.*', 'planets.*', 'images.path', 'images.name as imageName')
.where('astronauts.id', id).first();
const data = await knex('astronauts')
.select('astronauts.*', 'planets.*', 'images.path', 'images.name as imageName')
.join('planets', 'planets.id', '=', 'astronauts.originPlanetId')
.join('images', 'images.id', '=', 'planets.imageId')
.where('astronauts.id', id)
.first();
if (data) {
res.status(200).json({
id: data.id,
Expand Down Expand Up @@ -59,7 +67,10 @@ const AstronautController = {
try {
const [id] = await knex.insert({ firstname, lastname, originPlanetId }).into('astronauts');
res.status(200).json({
id, firstname, lastname, originPlanetId,
id,
firstname,
lastname,
originPlanetId,
});
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
Expand Down
4 changes: 3 additions & 1 deletion backend/src/entities/Astronaut.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Planet from './Planet';

interface Astronaut {
// WILSON A TOUT OUBLIÉ ICI
firstname: string;
lastname: string;
originPlanet: Planet;
}

export default Astronaut;

0 comments on commit a0ab542

Please sign in to comment.