-
Notifications
You must be signed in to change notification settings - Fork 0
Swati ProductList Api #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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') | ||
| } | ||
| let produsctsQuery = Product.query() | ||
| if (search) { | ||
| produsctsQuery = produsctsQuery.where((query) => | ||
| query.where('Lower(title)', 'Like', `[%${search.toLowerCase}%]`) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is incorrect....
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } | ||
| } | ||
| 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 | ||
| } |
| 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) | ||
| } | ||
| } |
| 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.`, | ||
| }, | ||
| ] | ||
| ) | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done