-
Notifications
You must be signed in to change notification settings - Fork 10
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
Showing
1 changed file
with
62 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Repository } from 'typeorm'; | ||
import { ProductVariant } from '../entities/ProductVariant'; | ||
import AppDataSource from '../config/ormconfig'; | ||
|
||
export class ProductVariantService { | ||
private repository: Repository<ProductVariant>; | ||
|
||
constructor() { | ||
this.repository = AppDataSource.getRepository(ProductVariant); | ||
} | ||
|
||
/** | ||
* Create a new ProductVariant | ||
* @param data Partial data to create a ProductVariant | ||
* @returns The created ProductVariant | ||
*/ | ||
async create(data: Partial<ProductVariant>): Promise<ProductVariant> { | ||
const productVariant = this.repository.create(data); | ||
return await this.repository.save(productVariant); | ||
} | ||
|
||
/** | ||
* Retrieve all ProductVariants | ||
* @returns An array of ProductVariant | ||
*/ | ||
async getAll(): Promise<ProductVariant[]> { | ||
return await this.repository.find({ relations: ['product'] }); | ||
} | ||
|
||
/** | ||
* Retrieve a ProductVariant by its ID | ||
* @param id ID of the ProductVariant | ||
* @returns The ProductVariant or null if not found | ||
*/ | ||
async getById(id: number): Promise<ProductVariant | null> { | ||
return await this.repository.findOne({ where: { id }, relations: ['product'] }); | ||
} | ||
|
||
/** | ||
* Update an existing ProductVariant | ||
* @param id ID of the ProductVariant to update | ||
* @param data Partial data to update the ProductVariant | ||
* @returns The updated ProductVariant or null if not found | ||
*/ | ||
async update(id: number, data: Partial<ProductVariant>): Promise<ProductVariant | null> { | ||
const productVariant = await this.getById(id); | ||
if (!productVariant) return null; | ||
|
||
Object.assign(productVariant, data); | ||
return await this.repository.save(productVariant); | ||
} | ||
|
||
/** | ||
* Delete a ProductVariant by its ID | ||
* @param id ID of the ProductVariant to delete | ||
* @returns True if deleted, otherwise false | ||
*/ | ||
async delete(id: number): Promise<boolean> { | ||
const result = await this.repository.delete(id); | ||
return result.affected === 1; | ||
} | ||
} |