Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mariocodecr committed Dec 17, 2024
1 parent 4590594 commit 65e4e80
Show file tree
Hide file tree
Showing 19 changed files with 517 additions and 262 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"license": "ISC",
"description": "",
"dependencies": {
"dotenv": "^16.4.7",
"axios": "^1.7.9",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"express": "^4.21.1",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"express-validator": "^7.2.0",
"pg": "^8.13.1",
"reflect-metadata": "^0.2.2",
Expand Down
126 changes: 94 additions & 32 deletions src/controllers/AttributeController.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,96 @@
// attribute.controller.ts
import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common';
import {Request, Response} from 'express';
import { AttributeService } from '../services/attribute.service';
import { Attribute } from '../entities/Attribute';

@Controller('attributes')
export class AttributeController {
constructor(private readonly attributeService: AttributeService) {}

@Post()
create(@Body() data: Partial<Attribute>) {
return this.attributeService.create(data);
}

@Get()
findAll() {
return this.attributeService.findAll();
}

@Get(':id')
findOne(@Param('id') id: number) {
return this.attributeService.findOne(id);
}

@Put(':id')
update(@Param('id') id: number, @Body() data: Partial<Attribute>) {
return this.attributeService.update(id, data);
}

@Delete(':id')
delete(@Param('id') id: number) {
return this.attributeService.delete(id);
}
import AppDataSource from '../config/ormconfig';

const attributeService = new AttributeService();


//create new attribute
export const createAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const attribute = await attributeService.create(req.body);
res
.status(201)
.json({ success: true, message: "Attribute Created Successfully", data: attribute });
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}


//get all attributes
export const getAllAttributes = async (req: Request, res: Response): Promise<void> => {
try {
const attributes = await attributeService.getAll();
res
.status(200)
.json({ success: true, message: "Attributes Retrieved Successfully", data: attributes });
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

//get attribute by id
export const getAttributeById = async (req: Request, res: Response): Promise<void> => {
try {
const attribute = await attributeService.getById(Number(req.params.id));
if (!attribute) {
res
.status(404)
.json({ success: false, message: "Attribute Not Found" });
} else {
res
.status(200)
.json({ success: true, message: "Attribute Retrieved Successfully", data: attribute });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

//update attribute
export const updateAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const attribute = await attributeService.update(Number(req.params.id), req.body)
if (!attribute) {
res
.status(404)
.json({ success: false, message: "Attribute Not Found" });
} else {
res
.status(200)
.json({ success: true, message: "Attribute Updated Successfully", data: attribute });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}


//delete attribute
export const deleteAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const success = await attributeService.delete(Number(req.params.id));
if (!success) {
res
.status(404)
.json({ success: false, message: "Attribute Not Found" });
} else {
res
.status(200)
.send()
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}
119 changes: 92 additions & 27 deletions src/controllers/AttributeValueController.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,98 @@
import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common';
import { AttributeValueService } from '../services/attributeValue.service';
import { AttributeValue } from '../entities/AttributeValue';
import {Request, Response} from "express";
import { AttributeValueService } from "../services/attributeValue.service";

@Controller('attribute-values')
export class AttributeValueController {
constructor(private readonly attributeValueService: AttributeValueService) {}
const attributeValueService = new AttributeValueService();

@Post()
create(@Body() data: Partial<AttributeValue>) {
return this.attributeValueService.create(data);
}

@Get()
findAll() {
return this.attributeValueService.findAll();
}
//create new attribute value
export const createAttributeValue = async (req: Request, res: Response): Promise<void> => {
try {
const attributeValue = await attributeValueService.create(req.body);
if (!attributeValue) {
res
.status(400)
.json({ success: false, message: "Attribute not found" });
} else {
res
.status(201)
.json({ success: true, message: "Attribute Value Created Successfully", data: attributeValue });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

@Get(':id')
findOne(@Param('id') id: number) {
return this.attributeValueService.findOne(id);
}
//get all attribute values
export const getAllAttributesValues = async (req: Request, res: Response): Promise<void> => {
try {
const attributeValues = await attributeValueService.getAll();
res
.status(200)
.json({ success: true, message: "Attribute Values Retrieved Successfully", data: attributeValues });
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

@Put(':id')
update(@Param('id') id: number, @Body() data: Partial<AttributeValue>) {
return this.attributeValueService.update(id, data);
}
//get attribute value by id
export const getAttributeValueById = async (req: Request, res: Response): Promise<void> => {
try {
const attributeValue = await attributeValueService.getById(Number(req.params.id));
if (!attributeValue) {
res
.status(404)
.json({ success: false, message: "Attribute Value Not Found" });
} else {
res
.status(200)
.json({ success: true, message: "Attribute Value Retrieved Successfully", data: attributeValue });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

@Delete(':id')
delete(@Param('id') id: number) {
return this.attributeValueService.delete(id);
}
//update attribute value
export const updateAttributeValue = async (req: Request, res: Response): Promise<void> => {
try {
const attributeValue = await attributeValueService.update(Number(req.params.id), req.body)
if (!attributeValue) {
res
.status(404)
.json({ success: false, message: "Attribute Value Not Found" });
} else {
res
.status(200)
.json({ success: true, message: "Attribute Value Updated Successfully", data: attributeValue });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

//delete attribute value
export const deleteAttributeValue = async (req: Request, res: Response): Promise<void> => {
try {
const success = await attributeValueService.delete(Number(req.params.id));
if (!success) {
res
.status(404)
.json({ success: false, message: "Attribute Value Not Found" });
} else {
res
.status(204)
.send()
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}
35 changes: 0 additions & 35 deletions src/controllers/ProductVariantController.ts

This file was deleted.

72 changes: 72 additions & 0 deletions src/controllers/productVariantAttributeController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Request, Response } from 'express';
import { ProductVariantAttributeService as ProductVariantAttributeServiceClass } from '../services/productVariantAttribute.service';
import AppDataSource from '../config/ormconfig';

const ProductVariantAttributeService = new ProductVariantAttributeServiceClass();

//create product variant attribute
export const createProductVariantAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const productVariantAttribute = await ProductVariantAttributeService.create(req.body);
if (!productVariantAttribute) {
res.status(400).json({ success: false, message: 'Invalid productVariantId or attributeValueId' });
} else {
res.status(201).json({ success: true, message: 'Product Variant Attribute Created Successfully', data: productVariantAttribute });
}
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}


//get all product variant attributes
export const getAllProductVariantAttributes = async (_req: Request, res: Response): Promise<void> => {
try {
const productVariantAttributes = await ProductVariantAttributeService.getAll();
res.status(200).json({ success: true, message: 'Product Variant Attributes Retrieved', data: productVariantAttributes });
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}

//get product variant attribute by id
export const getProductVariantAttributeById = async (req: Request, res: Response): Promise<void> => {
try {
const productVariantAttribute = await ProductVariantAttributeService.getById(Number(req.params.id));
if (!productVariantAttribute) {
res.status(404).json({ success: false, message: 'Product Variant Attribute not found' });
} else {
res.status(200).json({ success: true, message: 'Product Variant Attribute Retrieved Successfully', data: productVariantAttribute });
}
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}

//update product variant attribute
export const updateProductVariantAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const productVariantAttribute = await ProductVariantAttributeService.update(Number(req.params.id), req.body);
if (!productVariantAttribute) {
res.status(404).json({ success: false, message: 'Product Variant Attribute not found or invalid data' });
} else {
res.status(200).json({ success: true, message: 'Product Variant Attribute Updated Successfully', data: productVariantAttribute });
}
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}

//delete product variant attribute
export const deleteProductVariantAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const success = await ProductVariantAttributeService.delete(Number(req.params.id));
if (!success) {
res.status(404).json({ success: false, message: 'Product Variant Attribute not found' });
} else {
res.status(204).send();
}
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}
4 changes: 2 additions & 2 deletions src/entities/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export class Attribute {
@PrimaryGeneratedColumn()
id: number;

@Column({ unique: true })
@Column()
name: string;

@CreateDateColumn()
createdAt: Date;
}
}
Loading

0 comments on commit 65e4e80

Please sign in to comment.