Commit 274a3303 authored by nanahira's avatar nanahira

cond

parent fb3c9352
Pipeline #7299 passed with stages
in 1 minute and 26 seconds
...@@ -2,6 +2,7 @@ import { ConsoleLogger } from '@nestjs/common'; ...@@ -2,6 +2,7 @@ import { ConsoleLogger } from '@nestjs/common';
import { ClassConstructor } from 'class-transformer'; import { ClassConstructor } from 'class-transformer';
import { import {
DeleteResult, DeleteResult,
FindConditions,
IsNull, IsNull,
Not, Not,
Repository, Repository,
...@@ -149,10 +150,20 @@ export class CrudBase< ...@@ -149,10 +150,20 @@ export class CrudBase<
} }
} }
async update(id: EntityId<T>, entPart: Partial<T>) { async update(
id: EntityId<T>,
entPart: Partial<T>,
cond: FindConditions<T> = {},
) {
let result: UpdateResult; let result: UpdateResult;
try { try {
result = await this.repo.update(id, entPart); result = await this.repo.update(
{
id,
...cond,
},
entPart,
);
} catch (e) { } catch (e) {
this.error( this.error(
`Failed to update entity ID ${id} to ${JSON.stringify( `Failed to update entity ID ${id} to ${JSON.stringify(
...@@ -167,12 +178,20 @@ export class CrudBase< ...@@ -167,12 +178,20 @@ export class CrudBase<
return new BlankReturnMessageDto(200, 'success'); return new BlankReturnMessageDto(200, 'success');
} }
async remove(id: EntityId<T>, hardDelete = false) { async remove(
id: EntityId<T>,
hardDelete = false,
cond: FindConditions<T> = {},
) {
let result: UpdateResult | DeleteResult; let result: UpdateResult | DeleteResult;
const searchCond = {
id,
...cond,
};
try { try {
result = await (hardDelete result = await (hardDelete
? this.repo.delete(id) ? this.repo.delete(searchCond)
: this.repo.softDelete(id)); : this.repo.softDelete(searchCond));
} catch (e) { } catch (e) {
this.error(`Failed to delete entity ID ${id}: ${e.toString()}`); this.error(`Failed to delete entity ID ${id}: ${e.toString()}`);
throw new BlankReturnMessageDto(500, 'internal error').toException(); throw new BlankReturnMessageDto(500, 'internal error').toException();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment