Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions productos-km0-api/app/Controllers/Http/TransactionController.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
10 changes: 10 additions & 0 deletions productos-km0-api/app/Models/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export default class Product extends BaseModel {
})
public users: ManyToMany<typeof User>

@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<typeof User>

@column.dateTime({ autoCreate: true })
public createdAt: DateTime

Expand Down
11 changes: 11 additions & 0 deletions productos-km0-api/app/Models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ export default class User extends BaseModel {
})
public products: ManyToMany<typeof Product>

@manyToMany(() => Product, {
localKey: 'id',
pivotForeignKey: 'buyer_id',
relatedKey: 'id',
pivotRelatedForeignKey: 'product_id',
pivotTable: 'product_buyer',
pivotTimestamps: true,
pivotColumns: ['totalPrice', 'status', 'quantity'],
})
public productBuyer: ManyToMany<typeof Product>

@hasMany(() => Calendar, { foreignKey: 'sellerId' })
public calendar: HasMany<typeof Calendar>

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
107 changes: 107 additions & 0 deletions productos-km0-api/docs/swagger/controllers/transaction.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions productos-km0-api/docs/swagger/models/productBuyer.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions productos-km0-api/start/routes/transactions.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import Route from '@ioc:Adonis/Core/Route'

export default () => {
Route.resource('transactions', 'TransactionController')
Route.post('/:id/transactions', 'TransactionController.store')
}