22 Injectable ,
33 NotFoundException ,
44 ForbiddenException ,
5- InternalServerErrorException ,
65} from '@nestjs/common' ;
76import { PrismaService } from './../prisma/prisma.service' ;
87import { CreateFarmDto } from './dto/create-farm.dto' ;
@@ -12,73 +11,50 @@ export class FarmService {
1211 constructor ( private prisma : PrismaService ) { }
1312
1413 async createFarm ( createFarmDto : CreateFarmDto , userId : number ) {
15- try {
16- const existingFarm = await this . prisma . farm . findUnique ( {
17- where : { user_id : userId } ,
18- } ) ;
14+ const existingFarm = await this . prisma . farm . findUnique ( {
15+ where : { user_id : userId } ,
16+ } ) ;
1917
20- if ( existingFarm ) {
21- throw new ForbiddenException ( 'User already has a farm' ) ;
22- }
23- return await this . prisma . farm . create ( {
24- data : {
25- name : createFarmDto . name ,
26- location : createFarmDto . location ,
27- area_unit : createFarmDto . areaUnit ,
28- total_area : createFarmDto . totalArea ,
29- user : { connect : { id : userId } } ,
30- } ,
31- } ) ;
32- } catch ( error ) {
33- if ( error instanceof ForbiddenException ) throw error ;
34- throw new InternalServerErrorException ( error , 'Error creating farm' ) ;
18+ if ( existingFarm ) {
19+ throw new ForbiddenException ( 'User already has a farm' ) ;
3520 }
21+ return await this . prisma . farm . create ( {
22+ data : {
23+ name : createFarmDto . name ,
24+ location : createFarmDto . location ,
25+ area_unit : createFarmDto . areaUnit ,
26+ total_area : createFarmDto . totalArea ,
27+ user : { connect : { id : userId } } ,
28+ } ,
29+ } ) ;
3630 }
3731
3832 async getFarmByUserId ( userId : number ) {
39- try {
40- const farm = await this . prisma . farm . findUnique ( {
41- where : { user_id : userId } ,
42- } ) ;
43- if ( ! farm ) throw new NotFoundException ( 'Farm not found' ) ;
44- return farm ;
45- } catch ( error ) {
46- if ( error instanceof NotFoundException ) throw error ;
47- throw new InternalServerErrorException (
48- error ,
49- 'Error fetching farm by user ID' ,
50- ) ;
51- }
33+ const farm = await this . prisma . farm . findUnique ( {
34+ where : { user_id : userId } ,
35+ } ) ;
36+ if ( ! farm ) throw new NotFoundException ( 'Farm not found' ) ;
37+ return farm ;
5238 }
5339
5440 async update ( updateFarmDto : UpdateFarmDto , userId : number ) {
55- try {
56- const farm = await this . getFarmByUserId ( userId ) ;
41+ const farm = await this . getFarmByUserId ( userId ) ;
5742
58- return await this . prisma . farm . update ( {
59- where : { user_id : userId } ,
60- data : {
61- name : updateFarmDto . name ?? farm . name ,
62- total_area : updateFarmDto . totalArea ?? farm . total_area ,
63- location : updateFarmDto . location ?? farm . location ,
64- area_unit : updateFarmDto . areaUnit ?? farm . area_unit ,
65- } ,
66- } ) ;
67- } catch ( error ) {
68- if ( error instanceof NotFoundException ) throw error ;
69- throw new InternalServerErrorException ( error , 'Error updating farm' ) ;
70- }
43+ return await this . prisma . farm . update ( {
44+ where : { user_id : userId } ,
45+ data : {
46+ name : updateFarmDto . name ?? farm . name ,
47+ total_area : updateFarmDto . totalArea ?? farm . total_area ,
48+ location : updateFarmDto . location ?? farm . location ,
49+ area_unit : updateFarmDto . areaUnit ?? farm . area_unit ,
50+ } ,
51+ } ) ;
7152 }
7253
7354 async remove ( userId : number ) {
74- try {
75- await this . getFarmByUserId ( userId ) ;
76- return await this . prisma . farm . delete ( {
77- where : { user_id : userId } ,
78- } ) ;
79- } catch ( error ) {
80- if ( error instanceof NotFoundException ) throw error ;
81- throw new InternalServerErrorException ( error , 'Error deleting farm' ) ;
82- }
55+ await this . getFarmByUserId ( userId ) ;
56+ return await this . prisma . farm . delete ( {
57+ where : { user_id : userId } ,
58+ } ) ;
8359 }
8460}
0 commit comments