diff --git a/productos-km0-api/app/Controllers/Http/TransactionController.ts b/productos-km0-api/app/Controllers/Http/TransactionController.ts index c6aec36..04c6809 100644 --- a/productos-km0-api/app/Controllers/Http/TransactionController.ts +++ b/productos-km0-api/app/Controllers/Http/TransactionController.ts @@ -1,3 +1,49 @@ -// import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import Database from '@ioc:Adonis/Lucid/Database' +import Product from 'App/Models/Product' +import User from 'App/Models/User' +import PaginationValidator from 'App/Validators/PaginationValidator' -export default class TransactionsController {} +export default class TransactionsController { + public async index({ request, response }: HttpContextContract) { + const { page = 1, size = 10 } = await request.validate(PaginationValidator) + + const transaccion = await Database.from('product_buyer') + .innerJoin('products', 'product_buyer.product_id', '=', 'products.id') + .paginate(page, size) + + response.ok({ transaccion }) + } + + public async store({ request, response, params: { id } }: HttpContextContract) { + const user = await User.findOrFail(id) + const productId = await request.input('product_id') + const precio = await request.input('price') + const quantity = await request.input('quantity') + const product = await Product.find(productId) + const priceTotal = precio * quantity + + const transaccion = await user.related('productBuyer').attach({ + [product.id]: { + quantity: `${quantity}`, + totalPrice: `${priceTotal}`, + }, + }) + + response.ok({ transaccion }) + } + + public async show({ response, params: { id } }: HttpContextContract) { + const transaccion = await Database.from('product_buyer') + .innerJoin('products', 'product_buyer.product_id', '=', 'products.id') + .where('product_buyer.id', '=', `${id}`) + + response.ok({ transaccion }) + } + + public async destroy({ response, params: { id } }: HttpContextContract) { + await Database.from('product_buyer').where('id', '=', `${id}`).delete() + + response.ok(null) + } +} diff --git a/productos-km0-api/app/Models/Product.ts b/productos-km0-api/app/Models/Product.ts index 7301ad4..d604396 100644 --- a/productos-km0-api/app/Models/Product.ts +++ b/productos-km0-api/app/Models/Product.ts @@ -28,6 +28,16 @@ export default class Product extends BaseModel { }) public users: ManyToMany + @manyToMany(() => User, { + localKey: 'id', + pivotForeignKey: 'product_id', + relatedKey: 'id', + pivotRelatedForeignKey: 'buyer_id', + pivotTable: 'product_buyer', + pivotColumns: ['product_id', 'buyer_id', 'status', 'quantity', 'totalPrice'], + }) + public userBuyer: ManyToMany + @column.dateTime({ autoCreate: true }) public createdAt: DateTime diff --git a/productos-km0-api/app/Models/User.ts b/productos-km0-api/app/Models/User.ts index 0cfd501..c577d33 100644 --- a/productos-km0-api/app/Models/User.ts +++ b/productos-km0-api/app/Models/User.ts @@ -63,6 +63,17 @@ export default class User extends BaseModel { }) public products: ManyToMany + @manyToMany(() => Product, { + localKey: 'id', + pivotForeignKey: 'buyer_id', + relatedKey: 'id', + pivotRelatedForeignKey: 'product_id', + pivotTable: 'product_buyer', + pivotTimestamps: true, + pivotColumns: ['totalPrice', 'status', 'quantity'], + }) + public productBuyer: ManyToMany + @hasMany(() => Calendar, { foreignKey: 'sellerId' }) public calendar: HasMany diff --git a/productos-km0-api/database/migrations/1663182019330_product_buyers.ts b/productos-km0-api/database/migrations/1663182019330_product_buyers.ts new file mode 100644 index 0000000..5b3b6d6 --- /dev/null +++ b/productos-km0-api/database/migrations/1663182019330_product_buyers.ts @@ -0,0 +1,22 @@ +import BaseSchema from '@ioc:Adonis/Lucid/Schema' + +export default class extends BaseSchema { + protected tableName = 'product_buyer' + + public async up() { + this.schema.createTable(this.tableName, (table) => { + table.increments('id') + table.integer('buyer_id').unsigned().references('users.id') + table.integer('product_id').unsigned().references('products.id') + table.double('totalPrice') + table.double('quantity') + table.tinyint('status').defaultTo(1) + table.timestamp('created_at', { useTz: true }) + table.timestamp('updated_at', { useTz: true }) + }) + } + + public async down() { + this.schema.dropTable(this.tableName) + } +} diff --git a/productos-km0-api/docs/swagger/controllers/transaction.yml b/productos-km0-api/docs/swagger/controllers/transaction.yml new file mode 100644 index 0000000..3963d7e --- /dev/null +++ b/productos-km0-api/docs/swagger/controllers/transaction.yml @@ -0,0 +1,107 @@ +/api/v1/transactions: + get: + tags: + - Transaction + security: [] + description: List All Transactions + parameters: + - name: page + in: query + description: "Page to be listed" + required: false + - name: size + in: query + description: "Size per page to be listed" + required: false + produces: + - application/jsons + responses: + 200: + description: Transactions list + content: + application/json: + schema: + type: object + properties: + meta: + type: object + $ref: "#/components/schemas/Result" + data: + type: object + items: + $ref: "#/components/schemas/product_buyer" + 401: + description: Unauthorized +/api/v1/{userId}/transactions: + post: + tags: + - Transaction + security: [] + description: Create product sale + parameters: + - name: id + in: query + description: "User id buys the product" + required: true + - name: Request + in: body + required: true + schema: + properties: + product_id: + type: number + example: 1 + price: + type: number + example: 2.5 + quantity: + type: number + example: 2 + produces: + - application/json + responses: + 200: + description: Transaction created +/api/v1/transactions/{id}: + get: + tags: + - Transaction + security: [] + description: List Transaction by ID + parameters: + - name: id + in: query + description: "Transaction id to be listed" + required: true + produces: + - application/jsons + responses: + 200: + description: Return Transaction by id + content: + application/json: + schema: + $ref: "#/components/schemas/product_buyer" + 401: + description: Unauthorized + 404: + description: Transaction not found + delete: + tags: + - Transaction + security: [] + description: Delete existing Transaction + parameters: + - name: id + in: query + description: "Transaction id to be delete" + required: true + produces: + - application/jsons + responses: + 200: + description: Transaction delete successfully + 401: + description: Unauthorized + 404: + description: Transaction not found diff --git a/productos-km0-api/docs/swagger/models/productBuyer.yml b/productos-km0-api/docs/swagger/models/productBuyer.yml new file mode 100644 index 0000000..d4649d7 --- /dev/null +++ b/productos-km0-api/docs/swagger/models/productBuyer.yml @@ -0,0 +1,25 @@ +components: + schemas: + product_buyer: + id: + type: integer + format: int64 + example: 1 + buyer_id: + type: integer + format: int64 + example: 1 + product_id: + type: integer + format: int64 + example: 1 + totalPrice: + type: double + example: 2.5 + quantity: + type: integer + format: int64 + example: 2 + status: + type: boolean + example: true diff --git a/productos-km0-api/start/routes/transactions.routes.ts b/productos-km0-api/start/routes/transactions.routes.ts index 99f677b..fa04b15 100644 --- a/productos-km0-api/start/routes/transactions.routes.ts +++ b/productos-km0-api/start/routes/transactions.routes.ts @@ -2,4 +2,5 @@ import Route from '@ioc:Adonis/Core/Route' export default () => { Route.resource('transactions', 'TransactionController') + Route.post('/:id/transactions', 'TransactionController.store') }