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

Ot.past donations #11

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import { AuthModule } from './auth/auth.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { DonationsModule } from './donations/donations.module';
import typeorm from './config/typeorm';

@Module({
Expand All @@ -22,6 +23,7 @@ import typeorm from './config/typeorm';
}),
UsersModule,
AuthModule,
DonationsModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
27 changes: 27 additions & 0 deletions apps/backend/src/donations/donation.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Column, Entity, PrimaryGeneratedColumn, Timestamp } from 'typeorm';
import { DonationStatus } from './types';

@Entity()
export class Donation {
@PrimaryGeneratedColumn()
id: number;

@Column({ type: 'text', array: true }) //FIX
restrictions: string[];

@Column({ type: 'timestamp' })
due_date: Timestamp;

@Column({ type: 'int' })
pantry_id: number;

//@Column({ type: 'varchar', length: 50 })
@Column({ type: 'enum', enum: DonationStatus })
status: string;

@Column({ type: 'text', nullable: true })
feedback: string;

@Column({ type: 'json' })
contents: JSON;
}
56 changes: 56 additions & 0 deletions apps/backend/src/donations/donations.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
UseGuards,
Query,
} from '@nestjs/common';
import { DonationsService } from './donations.service';
import { CreateDonationDto } from './dto/create-donation.dto';
import { UpdateDonationDto } from './dto/update-donation.dto';
import { FilterDonationsDto } from './dto/filter-donations.dto';
import { AuthGuard } from '@nestjs/passport';
import { Logger } from '@nestjs/common';
import { ValidationPipe } from '@nestjs/common';

@Controller('donations')
export class DonationsController {
constructor(private readonly donationsService: DonationsService) {}

@Get('orders')
filter(@Query(new ValidationPipe()) filterDonationsDto: FilterDonationsDto) {
return this.donationsService.filter(filterDonationsDto);
}

@Post()
create(@Body() createDonationDto: CreateDonationDto) {
return this.donationsService.create(createDonationDto);
}

@Get()
findAll() {
return this.donationsService.findAll();
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does +id do?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

@Patch(':id')
update(
@Param('id') id: string,
@Body() updateDonationDto: UpdateDonationDto,
) {
return this.donationsService.update(+id, updateDonationDto);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.donationsService.remove(+id);
}
}
12 changes: 12 additions & 0 deletions apps/backend/src/donations/donations.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { DonationsService } from './donations.service';
import { DonationsController } from './donations.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Donation } from './donation.entity';

@Module({
imports: [TypeOrmModule.forFeature([Donation])],
controllers: [DonationsController],
providers: [DonationsService],
})
export class DonationsModule {}
58 changes: 58 additions & 0 deletions apps/backend/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Injectable } from '@nestjs/common';
import { CreateDonationDto } from './dto/create-donation.dto';
import { UpdateDonationDto } from './dto/update-donation.dto';
import { FilterDonationsDto } from './dto/filter-donations.dto';
import { Donation } from './donation.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Between, In } from 'typeorm';

@Injectable()
export class DonationsService {
constructor(@InjectRepository(Donation) private repo: Repository<Donation>) {}

create(createDonationDto: CreateDonationDto) {
return 'This action adds a new donation';
}

findAll() {
return `This action returns all donations`;
}

findOne(id: number) {
return `This action returns a #${id} donation`;
}

update(id: number, updateDonationDto: UpdateDonationDto) {
return `This action updates a #${id} donation`;
}

remove(id: number) {
return `This action removes a #${id} donation`;
}

async filter(filterDonationsDto: FilterDonationsDto) {
let query = this.repo.createQueryBuilder('donation');
if (filterDonationsDto.pantry_ids != null) {
query = query.where('donation.pantry_id IN (:...ids)', {
ids: filterDonationsDto.pantry_ids,
});
}
if (filterDonationsDto.status != null) {
query = query.andWhere('donation.status = :status', {
status: filterDonationsDto.status,
});
}
if (filterDonationsDto.due_date_start != null) {
query = query.andWhere('donation.due_date >= :start', {
start: filterDonationsDto.due_date_start,
});
}
if (filterDonationsDto.due_date_end != null) {
query = query.andWhere('donation.due_date <= :end', {
end: filterDonationsDto.due_date_end,
});
}
const donations = await query.getMany();
return donations;
}
}
1 change: 1 addition & 0 deletions apps/backend/src/donations/dto/create-donation.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class CreateDonationDto {}
26 changes: 26 additions & 0 deletions apps/backend/src/donations/dto/filter-donations.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
IsDate,
IsDateString,
IsEnum,
IsInt,
IsOptional,
} from 'class-validator';
import { DonationStatus } from '../types';

export class FilterDonationsDto {
@IsDateString()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do @isdate() instead to match with the Date types in DTO

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this and it broke. Is there anything wrong with isDateString?

@IsOptional()
due_date_start: Date;

@IsDateString()
@IsOptional()
due_date_end: Date;

@IsInt({ each: true })
@IsOptional()
pantry_ids: number[];

@IsEnum(DonationStatus)
@IsOptional()
status: string;
}
4 changes: 4 additions & 0 deletions apps/backend/src/donations/dto/update-donation.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateDonationDto } from './create-donation.dto';

export class UpdateDonationDto extends PartialType(CreateDonationDto) {}
6 changes: 6 additions & 0 deletions apps/backend/src/donations/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum DonationStatus {
REQUESTED = 'REQUESTED',
PROCESSING = 'PROCESSING',
IN_DELIVERY = 'IN_DELIVERY',
RECEIVED = 'RECEIVED',
}