Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some fields to crag and sector entity needed on crag info page #167

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { PropertyType } from './crags/entities/property-type.entity';
import EntityCacheSubscriber from './core/utils/entity-cache/entity-cache.subscriber';
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
import { BullModule } from '@nestjs/bull';
import { Parking } from './crags/entities/parking.entity';

@Module({
imports: [
Expand Down Expand Up @@ -98,6 +99,7 @@ import { BullModule } from '@nestjs/bull';
CragProperty,
IceFallProperty,
PropertyType,
Parking,
],
// subscribers: [EntityCacheSubscriber],
// logging: ['query', 'error'],
Expand Down
4 changes: 4 additions & 0 deletions src/crags/crags.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import { SummaryQueueConsumer } from './consumers/summary-queue.consumer';
import { CragLoader } from './loaders/crag.loader';
import { RouteLoader } from './loaders/route.loader';
import { RouteEvent } from './entities/route-event.entity';
import { Parking } from './entities/parking.entity';
import { ParkingsService } from './services/parkings.service';

@Module({
imports: [
Expand All @@ -94,6 +96,7 @@ import { RouteEvent } from './entities/route-event.entity';
CragProperty,
IceFallProperty,
StarRatingVote,
Parking,
]),
forwardRef(() => AuditModule),
BullModule.registerQueue({
Expand Down Expand Up @@ -142,6 +145,7 @@ import { RouteEvent } from './entities/route-event.entity';
ConfigService,
CragLoader,
RouteLoader,
ParkingsService,
],
controllers: [UploadController],
exports: [
Expand Down
84 changes: 83 additions & 1 deletion src/crags/entities/crag.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ import {
ManyToMany,
JoinTable,
Index,
OneToOne,
JoinColumn,
} from 'typeorm';
import { ObjectType, Field, Int, Float } from '@nestjs/graphql';
import {
ObjectType,
Field,
Int,
Float,
registerEnumType,
} from '@nestjs/graphql';
import { Country } from './country.entity';
import { Sector } from './sector.entity';
import { Area } from './area.entity';
Expand All @@ -31,6 +39,40 @@ export enum CragType {
ALPINE = 'alpine',
}

export enum Orientation {
NORTH = 'north',
NORTHEAST = 'northeast',
EAST = 'east',
SOUTHEAST = 'southeast',
SOUTH = 'south',
SOUTHWEST = 'southwest',
WEST = 'west',
NORTHWEST = 'northwest',
}
registerEnumType(Orientation, {
name: 'Orientation',
});

export enum WallAngle {
SLAB = 'slab',
VERTICAL = 'vertical',
OVERHANG = 'overhang',
ROOF = 'roof',
}
registerEnumType(WallAngle, {
name: 'WallAngle',
});

export enum Season {
SPRING = 'spring',
SUMMER = 'summer',
AUTUMN = 'autumn',
WINTER = 'winter',
}
registerEnumType(Season, {
name: 'Season',
});

@Entity()
@Index(['publishStatus'])
@ObjectType()
Expand Down Expand Up @@ -181,4 +223,44 @@ export class Crag extends BaseEntity {
})
@Field(() => [Int])
activityByMonth: number[];

@Column({
type: 'enum',
enum: Orientation,
array: true,
nullable: true,
})
@Field(() => [Orientation], { nullable: true })
orientations: Orientation[];

@Column({ nullable: true })
@Field(() => Int, { nullable: true })
approachTime: number;

@Column({
type: 'enum',
enum: WallAngle,
array: true,
nullable: true,
})
@Field(() => [WallAngle], { nullable: true })
wallAngles: WallAngle[];

@Column({
type: 'enum',
enum: Season,
array: true,
nullable: true,
})
@Field(() => [Season], { nullable: true })
seasons: Season[];

@Column({ nullable: true })
@Field({ nullable: true })
rainProof: boolean;

@OneToOne(() => Image, { nullable: true })
@JoinColumn()
@Field(() => Image, { nullable: true })
coverImage: Promise<Image>;
}
30 changes: 30 additions & 0 deletions src/crags/entities/parking.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Field, Float, ObjectType } from '@nestjs/graphql';
import {
BaseEntity,
Column,
Entity,
ManyToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Sector } from './sector.entity';

@Entity()
@ObjectType()
export class Parking extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
@Field()
id: string;

@Column({ type: 'float' })
@Field(() => Float)
lat: number;

@Column({ type: 'float' })
@Field(() => Float)
lon: number;

@ManyToMany(() => Sector, (sector) => sector.parkings, {
onDelete: 'CASCADE',
})
sectors: Sector[];
}
7 changes: 7 additions & 0 deletions src/crags/entities/sector.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import {
BaseEntity,
ManyToOne,
OneToMany,
ManyToMany,
JoinTable,
} from 'typeorm';
import { ObjectType, Field } from '@nestjs/graphql';
import { Crag } from '../../crags/entities/crag.entity';
import { Route } from '../../crags/entities/route.entity';
import { User } from '../../users/entities/user.entity';
import { EntityStatus } from './enums/entity-status.enum';
import { PublishStatus } from './enums/publish-status.enum';
import { Parking } from './parking.entity';

@Entity()
@ObjectType()
Expand Down Expand Up @@ -84,4 +87,8 @@ export class Sector extends BaseEntity {
user: Promise<User>;
@Column({ name: 'user_id', nullable: true })
userId: string;

@ManyToMany(() => Parking, (parking) => parking.sectors)
@JoinTable()
parkings: Parking[];
}
10 changes: 8 additions & 2 deletions src/crags/resolvers/sectors.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { UserAuthGuard } from '../../auth/guards/user-auth.guard';
import { ForeignKeyConstraintFilter } from '../filters/foreign-key-constraint.filter';
import { CurrentUser } from '../../auth/decorators/current-user.decorator';
import { User } from '../../users/entities/user.entity';
import { RoutesService } from '../services/routes.service';
import { NotificationService } from '../../notification/services/notification.service';
import { SectorRoutesLoader } from '../loaders/sector-routes.loader';
import DataLoader from 'dataloader';
Expand All @@ -36,15 +35,17 @@ import {
} from '../../core/interceptors/data-loader.interceptor';
import { CragsService } from '../services/crags.service';
import { UserLoader } from '../../users/loaders/user.loader';
import { Parking } from '../entities/parking.entity';
import { ParkingsService } from '../services/parkings.service';

@Resolver(() => Sector)
@UseInterceptors(DataLoaderInterceptor)
export class SectorsResolver {
constructor(
private cragsService: CragsService,
private sectorsService: SectorsService,
private routesService: RoutesService,
private notificationService: NotificationService,
private parkingsService: ParkingsService,
) {}

/* QUERIES */
Expand Down Expand Up @@ -195,4 +196,9 @@ export class SectorsResolver {
): Promise<User> {
return sector.userId ? loader.load(sector.userId) : null;
}

@ResolveField('parkings', () => [Parking])
async getParkings(@Parent() sector: Sector): Promise<Parking[]> {
return this.parkingsService.getParkings(sector.id);
}
}
20 changes: 20 additions & 0 deletions src/crags/services/parkings.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Parking } from '../entities/parking.entity';
import { Repository } from 'typeorm';

@Injectable()
export class ParkingsService {
constructor(
@InjectRepository(Parking) private parkingsRepository: Repository<Parking>,
) {}

async getParkings(sectorId: string): Promise<Parking[]> {
const qb = this.parkingsRepository
.createQueryBuilder('parking')
.leftJoin('parking.sectors', 'sector')
.where('sector.id = :sectorId', { sectorId });

return qb.getMany();
}
}
16 changes: 16 additions & 0 deletions src/migration/1696868866474-addOrientationsEnumArrayField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class addOrientationsEnumArrayField1696868866474 implements MigrationInterface {
name = 'addOrientationsEnumArrayField1696868866474'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TYPE "public"."crag_orientations_enum" AS ENUM('north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'northwest')`);
await queryRunner.query(`ALTER TABLE "crag" ADD "orientations" "public"."crag_orientations_enum" array`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "orientations"`);
await queryRunner.query(`DROP TYPE "public"."crag_orientations_enum"`);
}

}
14 changes: 14 additions & 0 deletions src/migration/1696876275575-addApproachTimeField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class addApproachTimeField1696876275575 implements MigrationInterface {
name = 'addApproachTimeField1696876275575'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "crag" ADD "approach_time" integer`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "approach_time"`);
}

}
16 changes: 16 additions & 0 deletions src/migration/1696963335256-addWallAnglesEnumArrayField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class addWallAnglesEnumArrayField1696963335256 implements MigrationInterface {
name = 'addWallAnglesEnumArrayField1696963335256'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TYPE "public"."crag_wall_angles_enum" AS ENUM('slab', 'vertical', 'overhang', 'roof')`);
await queryRunner.query(`ALTER TABLE "crag" ADD "wall_angles" "public"."crag_wall_angles_enum" array`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "wall_angles"`);
await queryRunner.query(`DROP TYPE "public"."crag_wall_angles_enum"`);
}

}
18 changes: 18 additions & 0 deletions src/migration/1697038709137-addSeasonsAndRainProofFields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class addSeasonsAndRainProofFields1697038709137 implements MigrationInterface {
name = 'addSeasonsAndRainProofFields1697038709137'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TYPE "public"."crag_seasons_enum" AS ENUM('spring', 'summer', 'autumn', 'winter')`);
await queryRunner.query(`ALTER TABLE "crag" ADD "seasons" "public"."crag_seasons_enum" array`);
await queryRunner.query(`ALTER TABLE "crag" ADD "rain_proof" boolean`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "rain_proof"`);
await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "seasons"`);
await queryRunner.query(`DROP TYPE "public"."crag_seasons_enum"`);
}

}
18 changes: 18 additions & 0 deletions src/migration/1697179836611-addCoverImageFieldCrag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class addCoverImageFieldCrag1697179836611 implements MigrationInterface {
name = 'addCoverImageFieldCrag1697179836611'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "crag" ADD "cover_image_id" uuid`);
await queryRunner.query(`ALTER TABLE "crag" ADD CONSTRAINT "UQ_92ec8a92a0d6a2c152ed6b47e90" UNIQUE ("cover_image_id")`);
await queryRunner.query(`ALTER TABLE "crag" ADD CONSTRAINT "FK_92ec8a92a0d6a2c152ed6b47e90" FOREIGN KEY ("cover_image_id") REFERENCES "image"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "crag" DROP CONSTRAINT "FK_92ec8a92a0d6a2c152ed6b47e90"`);
await queryRunner.query(`ALTER TABLE "crag" DROP CONSTRAINT "UQ_92ec8a92a0d6a2c152ed6b47e90"`);
await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "cover_image_id"`);
}

}
24 changes: 24 additions & 0 deletions src/migration/1697385889286-addParkingEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class addParkingEntity1697385889286 implements MigrationInterface {
name = 'addParkingEntity1697385889286'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "parking" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "lat" double precision NOT NULL, "lon" double precision NOT NULL, CONSTRAINT "PK_d611d86b1d39963d048b05976aa" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "sector_parkings_parking" ("sector_id" uuid NOT NULL, "parking_id" uuid NOT NULL, CONSTRAINT "PK_9542e684019edec4d8f4783993d" PRIMARY KEY ("sector_id", "parking_id"))`);
await queryRunner.query(`CREATE INDEX "IDX_02ab9a0fee51cb95b32b602190" ON "sector_parkings_parking" ("sector_id") `);
await queryRunner.query(`CREATE INDEX "IDX_b115f5dc591dea45f4bb9c7f07" ON "sector_parkings_parking" ("parking_id") `);
await queryRunner.query(`ALTER TABLE "sector_parkings_parking" ADD CONSTRAINT "FK_02ab9a0fee51cb95b32b6021904" FOREIGN KEY ("sector_id") REFERENCES "sector"("id") ON DELETE CASCADE ON UPDATE CASCADE`);
await queryRunner.query(`ALTER TABLE "sector_parkings_parking" ADD CONSTRAINT "FK_b115f5dc591dea45f4bb9c7f070" FOREIGN KEY ("parking_id") REFERENCES "parking"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "sector_parkings_parking" DROP CONSTRAINT "FK_b115f5dc591dea45f4bb9c7f070"`);
await queryRunner.query(`ALTER TABLE "sector_parkings_parking" DROP CONSTRAINT "FK_02ab9a0fee51cb95b32b6021904"`);
await queryRunner.query(`DROP INDEX "public"."IDX_b115f5dc591dea45f4bb9c7f07"`);
await queryRunner.query(`DROP INDEX "public"."IDX_02ab9a0fee51cb95b32b602190"`);
await queryRunner.query(`DROP TABLE "sector_parkings_parking"`);
await queryRunner.query(`DROP TABLE "parking"`);
}

}
Loading