-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
578e9a6
commit bad6179
Showing
3 changed files
with
46 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
import { Parcel } from "@/typeorm/Entities/Parcel"; | ||
import { AppDataSource } from "@/appDataSource"; | ||
import { ErrorWithCode } from "@/utilities/customErrors/ErrorWithCode"; | ||
import { Parcel } from '@/typeorm/Entities/Parcel'; | ||
import { AppDataSource } from '@/appDataSource'; | ||
import { ErrorWithCode } from '@/utilities/customErrors/ErrorWithCode'; | ||
|
||
const parcelRepo = AppDataSource.getRepository(Parcel); | ||
|
||
/** | ||
* @description Adds a new parcel to the datasource. | ||
* @param parcel incoming parcel data to be added to the database | ||
* @returns {Response} A 201 status and the data of the role added. | ||
* @throws ErrorWithCode If the parcel already exists or is unable to be added. | ||
* @throws ErrorWithCode If the parcel already exists or is unable to be added. | ||
*/ | ||
export const postParcel = async (parcel: Parcel) => { | ||
const existingParcel = await getParcelById(parcel.Id); | ||
if (existingParcel) { | ||
throw new ErrorWithCode('Parcel already exists', 409); | ||
}; | ||
const newParcel = parcelRepo.save(parcel); | ||
return newParcel; | ||
const existingParcel = await getParcelById(parcel.Id); | ||
if (existingParcel) { | ||
throw new ErrorWithCode('Parcel already exists', 409); | ||
} | ||
const newParcel = parcelRepo.save(parcel); | ||
return newParcel; | ||
}; | ||
|
||
/** | ||
* @description Finds and returns a parcel with matching Id | ||
* @description Finds and returns a parcel with matching Id | ||
* @param parcelId Number representing parcel we want to find. | ||
* @returns findParcel Parcel data matching Id passed in. | ||
* @returns findParcel Parcel data matching Id passed in. | ||
*/ | ||
export const getParcelById = async (parcelId: number) => { | ||
try{ | ||
const findParcel = await parcelRepo.findOne({ | ||
where: { Id: parcelId}, | ||
}); | ||
return findParcel; | ||
} catch (e) { | ||
throw new ErrorWithCode(e.message, e.status); | ||
} | ||
} | ||
try { | ||
const findParcel = await parcelRepo.findOne({ | ||
where: { Id: parcelId }, | ||
}); | ||
return findParcel; | ||
} catch (e) { | ||
throw new ErrorWithCode(e.message, e.status); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters