Skip to content

Commit

Permalink
modify: add column "isDeleted" and handling removing
Browse files Browse the repository at this point in the history
  • Loading branch information
zinirun committed Mar 10, 2021
1 parent 36f94f1 commit fd7a362
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 6 deletions.
4 changes: 4 additions & 0 deletions backend/src/form/form.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class Form {
@Index({ unique: true })
pubUrl: string;

@Field(() => Boolean)
@Column({ default: false })
isDeleted: boolean;

/**
* Join with user -> userId
*/
Expand Down
2 changes: 1 addition & 1 deletion backend/src/form/form.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class FormRepository extends Repository<Form> {
async findOneByIdWithUser(id: number, userId: number) {
return await this.createQueryBuilder('form')
.leftJoinAndSelect('form.user', 'user')
.where('form.id = :id and form.userId = :userId', { id, userId })
.where('form.id = :id and form.userId = :userId and form.isDeleted = 0', { id, userId })
.getOne();
}
}
6 changes: 6 additions & 0 deletions backend/src/form/form.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export class FormResolver {
return await this.formService.publish(id, user);
}

@UseGuards(LoginGuard)
@Mutation(() => Boolean)
async removeForm(@GetUser() user: User, @Args('id') id: number): Promise<boolean> {
return await this.formService.remove(id, user);
}

@UseGuards(LoginGuard)
@Mutation(() => Form)
async updateFormStatusClosed(@GetUser() user: User, @Args('id') id: number): Promise<Form> {
Expand Down
25 changes: 23 additions & 2 deletions backend/src/form/form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export class FormService {
}

async getOnePublic(id: number): Promise<Form> {
const form = await this.formRepository.findOne(id);
const form = await this.formRepository.findOne(id, {
where: {
isDeleted: false,
},
});
if (!form) {
throw new NotFoundException(`Form with ID ${id}: Not Found`);
}
Expand All @@ -47,6 +51,7 @@ export class FormService {
const form = await this.formRepository.findOne({
where: {
pubUrl,
isDeleted: false,
},
});
if (!form) {
Expand All @@ -59,13 +64,14 @@ export class FormService {
return await this.formRepository.find({
where: {
group: groupId,
isDeleted: false,
},
});
}

async create(user: User, form: FormInput): Promise<Form> {
try {
const group = await this.groupService.getOne(form.groupId);
const group = await this.groupService.getOne(form.groupId, user);
const { id } = await this.formRepository.save({ ...form, user, group });
return await this.getOne(id, user);
} catch (err) {
Expand All @@ -86,6 +92,21 @@ export class FormService {
}
}

async remove(id: number, user: User): Promise<boolean> {
try {
await this.getOne(id, user);
await this.formRepository.update(
{ id },
{
isDeleted: true,
},
);
return true;
} catch (err) {
throw new ConflictException(err);
}
}

async updateStatusClosed(id: number, user: User): Promise<Form> {
try {
const { status } = await this.getOne(id, user);
Expand Down
4 changes: 4 additions & 0 deletions backend/src/group/group.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class Group {
@Column({ length: 40 })
name: string;

@Field(() => Boolean)
@Column({ default: false })
isDeleted: boolean;

/**
* Join with user -> userId
*/
Expand Down
6 changes: 6 additions & 0 deletions backend/src/group/group.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ export class GroupResolver {
async createGroup(@GetUser() user: User, @Args('group') group: GroupInput): Promise<Group> {
return await this.groupService.create(user, group);
}

@UseGuards(LoginGuard)
@Mutation(() => Boolean)
async removeGroup(@GetUser() user: User, @Args('id') id: number): Promise<boolean> {
return await this.groupService.remove(id, user);
}
}
27 changes: 24 additions & 3 deletions backend/src/group/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import { GroupInput } from './group.input';
export class GroupService {
constructor(@InjectRepository(Group) private groupRepository: Repository<Group>) {}

async getOne(id: number): Promise<Group> {
const group = await this.groupRepository.findOne(id);
async getOne(id: number, user: User): Promise<Group> {
const group = await this.groupRepository.findOne(id, {
where: {
userId: user.id,
isDeleted: false,
},
});
if (!group) {
throw new NotFoundException(`Group with ID ${id}: Not Found`);
}
Expand All @@ -21,14 +26,30 @@ export class GroupService {
return await this.groupRepository.find({
where: {
user: user.id,
isDeleted: false,
},
});
}

async create(user: User, group: GroupInput): Promise<Group> {
try {
const { id } = await this.groupRepository.save({ ...group, user });
return await this.getOne(id);
return await this.getOne(id, user);
} catch (err) {
throw new ConflictException(err);
}
}

async remove(id: number, user: User): Promise<boolean> {
try {
await this.getOne(id, user);
await this.groupRepository.update(
{ id },
{
isDeleted: true,
},
);
return true;
} catch (err) {
throw new ConflictException(err);
}
Expand Down
4 changes: 4 additions & 0 deletions backend/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Form {
content: String!
status: String!
pubUrl: String
isDeleted: Boolean!
user: User!
group: Group!
createdAt: Date!
Expand All @@ -30,6 +31,7 @@ type Form {
type Group {
id: Float!
name: String!
isDeleted: Boolean!
user: User!
createdAt: Date!
updatedAt: Date!
Expand Down Expand Up @@ -59,8 +61,10 @@ type Mutation {
createForm(form: FormInput!): Form!
updateForm(form: FormUpdateInput!, id: Float!): Form!
publishForm(id: Float!): Form!
removeForm(id: Float!): Boolean!
updateFormStatusClosed(id: Float!): Form!
createGroup(group: GroupInput!): Group!
removeGroup(id: Float!): Boolean!
createAnswer(answer: AnswerInput!): Answer!
}

Expand Down

0 comments on commit fd7a362

Please sign in to comment.