Skip to content

Commit

Permalink
Feature/address (#76)
Browse files Browse the repository at this point in the history
* feat: address #75

* feat: address #75

* fix: change of zip_code to zipcode
  • Loading branch information
AndreLZGava authored Nov 26, 2021
1 parent ecb948b commit 2fa6e17
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
Binary file modified src/database/database.sqlite
Binary file not shown.
84 changes: 84 additions & 0 deletions src/database/migration/1637865030266-create_address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
MigrationInterface,
QueryRunner,
Table,
TableForeignKey,
} from 'typeorm';

export class createAddress1637865030266 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'address',
columns: [
{
name: 'id',
type: 'int',
isPrimary: true,
},
{
name: 'complement',
type: 'varchar',
},
{
name: 'zipcode',
type: 'varchar',
},
{
name: 'neighborhood',
type: 'varchar',
},
{
name: 'public_place',
type: 'varchar',
},
{
name: 'city_id',
type: 'int',
},
{
name: 'user_id',
type: 'int',
},
],
}),
true
);

await queryRunner.createForeignKey(
'address',
new TableForeignKey({
columnNames: ['city_id'],
referencedColumnNames: ['id'],
referencedTableName: 'city',
onDelete: 'CASCADE',
})
);

await queryRunner.createForeignKey(
'address',
new TableForeignKey({
columnNames: ['user_id'],
referencedColumnNames: ['id'],
referencedTableName: 'user',
onDelete: 'CASCADE',
})
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable('address');

const userId = table.foreignKeys.find(
(fk) => fk.columnNames.indexOf('user_id') !== -1
);
await queryRunner.dropForeignKey('address', userId);

const cityId = table.foreignKeys.find(
(fk) => fk.columnNames.indexOf('city_id') !== -1
);
await queryRunner.dropForeignKey('address', cityId);

await queryRunner.dropTable('address');
}
}
2 changes: 2 additions & 0 deletions src/electron/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Permission } from './database/models/permission.schema';
import { Borrow } from './database/models/borrow.schema';
import { BorrowRenovation } from './database/models/borrow_renovation.schema';
import { City } from './database/models/city.schema';
import { Address } from './database/models/address.schema';

declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
Expand Down Expand Up @@ -67,6 +68,7 @@ export default class Main {
Publisher,
Region,
City,
Address,
Country,
Profile,
TypeUser,
Expand Down
32 changes: 32 additions & 0 deletions src/electron/database/models/address.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
import { City } from './city.schema';
import { User } from './user.schema';

@Entity()
export class Address {
@PrimaryGeneratedColumn()
id: number;
@Column()
complement: string;

@Column()
zipcode: string;

@Column()
neighborhood: string;

@Column()
public_place: string;

@Column()
city_id: number;

@Column()
user_id: number;

@ManyToOne(() => User, (user) => user.addresses)
user: User;

@ManyToOne(() => City, (city) => city.addresses)
city: City;
}
4 changes: 4 additions & 0 deletions src/electron/database/models/city.schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne } from 'typeorm';
import { Address } from './address.schema';
import { Region } from './region.schema';

@Entity()
Expand All @@ -15,4 +16,7 @@ export class City

@ManyToOne(() => Region, region => region.cities)
region: Region;

@OneToMany(() => Address, address => address.city)
addresses: Address[];
}
4 changes: 4 additions & 0 deletions src/electron/database/models/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Profile } from './profile.schema';
import { Contact } from './contact.schema';
import { TypeUser } from './type_user.schema';
import { Borrow } from './borrow.schema';
import { Address } from './address.schema';

@Entity()
export class User {
Expand Down Expand Up @@ -42,4 +43,7 @@ export class User {
@OneToMany(() => Borrow, borrow => borrow.user)
borrows: Borrow[];

@OneToMany(() => Address, address => address.user)
addresses: Address[];

}

0 comments on commit 2fa6e17

Please sign in to comment.