-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
Controller, | ||
Get, | ||
Post, | ||
Body, | ||
Patch, | ||
Param, | ||
Delete, | ||
UseGuards, | ||
} 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'; | ||
|
||
@Controller('donations') | ||
export class DonationsController { | ||
constructor(private readonly donationsService: DonationsService) {} | ||
|
||
@Get('orders') | ||
filter(@Body() 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does +id do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. converts to number representation of id |
||
} | ||
|
||
@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); | ||
} | ||
} |
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 {} |
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class CreateDonationDto {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
IsDate, | ||
IsDateString, | ||
IsEnum, | ||
IsInt, | ||
IsOptional, | ||
} from 'class-validator'; | ||
import { DonationStatus } from '../types'; | ||
import { Timestamp } from 'typeorm'; | ||
|
||
export class FilterDonationsDto { | ||
@IsDateString() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do @isdate() instead to match with the Date types in DTO There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
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) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum DonationStatus { | ||
FULFILLED = 'FULFILLED', | ||
UNFULFILLED = 'UNFULFILLED', | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to replace the @Body() with @query() because we usually do @query() with GET requests and @Body with POST or PUT
https://dev.to/shameel/nestjs-request-param-body-query-headers-ip-5e6k
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you mean instead of using a FilterDonationsDto to put all of the filter conditions in the url to use @query?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed