Skip to content

Commit

Permalink
country CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
anamontiaga committed May 28, 2024
1 parent c1f5a45 commit b685644
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 11 deletions.
6 changes: 4 additions & 2 deletions api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import * as config from '@shared/config/development.json';
type: 'postgres',
host: 'localhost',
port: 5432,
username: config.db.username,
password: config.db.password,
// username: config.db.username,
// password: config.db.password,
username: 'postgres',
password: 'postgres',
database: config.db.database,
entities: [User, Country],
synchronize: true,
Expand Down
43 changes: 38 additions & 5 deletions api/src/modules/countries/countries.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
import { Controller, Get } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
} from '@nestjs/common';
import { CountriesService } from './countries.service';
import { Country } from '@shared/dto/countries/country.entity';
import { CreateCountryDto } from '@shared/dto/countries/create-country.dto';

@Controller()
@Controller('countries')
export class CountriesController {
@Get('countries')
createCountry() {
return 'This action adds a new country';
constructor(private countriesService: CountriesService) {}

@Post()
async create(@Body() createCountryDto: CreateCountryDto) {
this.countriesService.create(createCountryDto);
}

@Get()
async findAll(): Promise<Country[]> {
return this.countriesService.findAll();
}

@Get(':id')
async findOne(@Param('id') id: string) {
return this.countriesService.findOne(+id);
}

@Put(':id')
async update(@Param('id') id: string) {
return this.countriesService.update(+id);
}

@Delete(':id')
async remove(@Param('id') id: string) {
return this.countriesService.remove(+id);
}
}
35 changes: 34 additions & 1 deletion api/src/modules/countries/countries.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
import { Injectable } from '@nestjs/common';
import { Country } from '@shared/dto/countries/country.entity';

@Injectable()
export class CountriesService {}
export class CountriesService {
private readonly country: Country[] = [];

create(country: Country) {
this.country.push(country);
}

findAll(): Country[] {
return this.country;
}

findOne(id: number) {
return this.country.find((country) => country.id === id);
}

update(id: number) {
const index = this.country.findIndex((country) => country.id === id);
if (index === -1) {
return 'Country not found';
}
this.country[index] = { ...this.country[index], name: 'Country updated' };
return 'Country updated';
}

remove(id: number) {
const index = this.country.findIndex((country) => country.id === id);
if (index === -1) {
return 'Country not found';
}
this.country.splice(index, 1);
return 'Country removed';
}
}
26 changes: 23 additions & 3 deletions api/src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';

@Controller()
export class UsersController {
@Get('users')
createUser() {
@Post('users')
create(): string {
return 'This action adds a new user';
}

@Get('users')
findAll(): string {
return 'This action returns all users';
}

@Get('users/:id')
findOne(@Param('id') id: string): string {
return `This action returns a #${id} user`;
}

@Put('users/:id')
update(@Param('id') id: string) {
return `This action updates a #${id} user`;
}

@Delete('users/:id')
remove(@Param('id') id: string) {
return `This action removes a #${id} user`;
}
}
2 changes: 2 additions & 0 deletions shared/dto/countries/country.entity.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
Column,
CreateDateColumn,
Entity,
OneToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import { User } from '@shared/dto/users/user.entity';

@Entity()
export class Country {
@PrimaryGeneratedColumn()
id: number;
Expand Down
22 changes: 22 additions & 0 deletions shared/dto/countries/create-country.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
import { User } from '../users/user.entity';

export class CreateCountryDto {
@IsNumber()
@IsNotEmpty()
id: number;

@IsString({ message: 'Name must be a string' })
@IsNotEmpty()
name: string;

@IsNotEmpty()
iso: string;

users: User[];

@IsNotEmpty()
createdAt: Date;
@IsNotEmpty()
updatedAt: Date;
}
2 changes: 2 additions & 0 deletions shared/dto/users/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
Column,
CreateDateColumn,
Entity,
ManyToOne,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';

import { Country } from '@shared/dto/countries/country.entity';

@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
Expand Down

0 comments on commit b685644

Please sign in to comment.