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
55 changes: 22 additions & 33 deletions app/Controllers/Http/ProductsController.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import products from 'App/Resourses/ProductsDummyData'

import Product from 'App/Models/Product'

export default class ProductsController {
public async index(ctx: HttpContextContract) {
const query = ctx.request.qs();
const page: number = query.page || 1;
const search: string = query.search || '';
const sortBy: string = query.sortBy || '';
const sortType: string = query.sortType || ''
let searchedProducts = products.filter((item) => {
return item.category.toLowerCase().includes(search.toLowerCase());
});
if (sortBy.toLowerCase() === "price") {
if (sortType.toLowerCase() === 'ascending') {
searchedProducts.sort(function (x, y) {
return x.price - y.price;
});
} else if (sortType.toLowerCase() === 'descending') {
searchedProducts.sort(function (x, y) {
return y.price - x.price;
});
}
} else if (sortBy.toLowerCase() === "title") {
if (sortType.toLowerCase() === 'ascending') {
searchedProducts.sort(function (x, y) {
return x.title < y.title ? -1 : 1;
})
} else if (sortType.toLowerCase() === 'descending') {
searchedProducts.sort(function (x, y) {
return x.title > y.title ? -1 : 1;
})
}
public async index({ request, response }: HttpContextContract) {
const { sortBy, sortType, page, search } = request.qs() as {
sortBy?: string
sortType?: string
page?: string
search?: string
}
if (sortType !== 'asc' && sortType !== 'desc' && sortType !== undefined) {
return response.badRequest('Try again')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the message should be more meaningful

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
let produsctsQuery = Product.query()
if (search) {
produsctsQuery = produsctsQuery.where((query) =>
query.where('Lower(title)', 'Like', `[%${search.toLowerCase}%]`)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is incorrect....

  1. toLowerCase isn't begin called
    2, The last arg is supposed to be the string... here you have given [%<user_input>%] this will not match with anything ever
  2. first arg takes a col name .... which "Lower(title)" is not

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okk

)
}
if (sortBy) {
produsctsQuery = produsctsQuery.orderBy(sortBy, sortType ? sortType : 'asc')
}
return searchedProducts.slice((page - 1) * 20, (page * 20));
const products = produsctsQuery.paginate(page ? +page : 1, 20)
return products
}
}
}
50 changes: 50 additions & 0 deletions app/Models/Product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { DateTime } from 'luxon'
import { BaseModel, BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm'
import User from './User'

export default class Product extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public title: string

@column()
public brand: string

@column()
public category: string

@column()
public description: string

@column()
public discountPercentage: string

@column()
public price: number

@column()
public rating: string

@column()
public stock: number

@column()
public image: string[]

@column()
public quantityId: number

@column()
public userId: number

@belongsTo(() => User)
public user: BelongsTo<typeof User>

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

@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime
}
30 changes: 30 additions & 0 deletions database/migrations/1665368585403_products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class extends BaseSchema {
protected tableName = 'products'

public async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments('id').unique()
table.string('title').notNullable()
table.string('description').notNullable()
table.string('brand').notNullable()
table.string('category').notNullable()
table.string('rating').nullable()
table.integer('price').notNullable()
table.integer('stock').nullable()
table.string('image').nullable()
table.integer('user_id').notNullable()

/**
* Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL
*/
table.timestamp('created_at', { useTz: true })
table.timestamp('updated_at', { useTz: true })
})
}

public async down() {
this.schema.dropTable(this.tableName)
}
}
58 changes: 58 additions & 0 deletions database/seeders/Product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'
import Product from 'App/Models/Product'

export default class extends BaseSeeder {
public async run() {
await Product.updateOrCreateMany(
['title', 'category'],
[
{
title: 'Apple Iphone 14',
id: 1,
brand: 'Apple',
userId: 2,
category: 'Mobile',
price: 100000,
description: `Longest battery life ever. A new Main camera and improved image processing let you capture even more sensational shots in all kinds of light — especially low light. Whether you’re filming while hiking up a rocky trail or chasing your kids through the park, try Action mode for smooth handheld videos. Safety features including emergency SOS via satellite, crash detection call for help when you can't.
`,
},
{
title: 'Samsung Galaxy S22',
id: 2,
brand: 'Samsung',
userId: 1,
category: 'Mobile',
price: 120000,
description: `Comes with Voice Focus, Vapour Cooling Chamber, Auto Data Switching and up to 16GB RAM. 6nm Dimensity 900 processor, Voice Focus, Vapour Cooling Chamber and up to 16GB RAM. 120Hz refresh rate. 5000mAh Battery. 6nm Octa-core processor. 6000mAh Battery.`,
},
{
title: 'Refrigrator',
id: 3,
brand: 'Samsung',
userId: 3,
category: 'Home Appliances',
price: 220000,
description: `Comes with Voice Focus, cbv nbxckmslm cvhdbcjdcnslkmz 5000mAh Battery. 6nm Octa-core processor. 6000mAh Battery.`,
},
{
title: 'Philips Iron',
id: 4,
brand: 'Philips',
userId: 5,
category: 'Appliances',
price: 2000,
description: 'cvhdghjclksjakx,mnsxb nmzxxlsdiwoeugfusiislxvb cgvhsdksjaswk xhcjskdj',
},
{
title: 'Television',
id: 5,
userId: 3,
brand: 'Sony',
category: 'Home Appliances',
price: 320000,
description: `Comes with Voice Focus, vchsbdkjsldk;sx vcgdhcjksksl 6000mAh Battery.`,
},
]
)
}
}
Binary file modified database/sqlite/db.sqlite3
Binary file not shown.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"dependencies": {
"@adonisjs/auth": "^8.2.2",
"@adonisjs/core": "^5.8.6",
"@adonisjs/lucid": "^18.1.1",
"@adonisjs/lucid": "^18.2.0",
"@adonisjs/repl": "^3.1.11",
"luxon": "^3.0.4",
"phc-argon2": "^1.1.3",
Expand Down
2 changes: 1 addition & 1 deletion start/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ Route.get('/', async () => {
Route.post('signup', 'AuthController.signup')
Route.post('login', 'AuthController.login')
Route.get('me', 'AuthController.me')
Route.get("/products", "ProductsController.index")
Route.get('/products', 'ProductsController.index')