Skip to content

Commit

Permalink
add API controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Herman-Riah19 committed Dec 12, 2023
1 parent 4ebaa22 commit e7e2d08
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
51 changes: 51 additions & 0 deletions app/Controllers/Http/ApiController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Comment from 'App/Models/Comment'
import Product from 'App/Models/Product'
import Profile from 'App/Models/Profile'
import User from 'App/Models/User'
import Drive from '@ioc:Adonis/Core/Drive'

export default class ApiController {
public async getAllUsers({session, response}: HttpContextContract) {
interface UserProfile{
user: User;
profile: Profile;
}
const allUsers = await User.all()
const allProfiles = await Profile.all()

const users = new Array<UserProfile>()

allUsers.map(user => {
allProfiles.map(profile => {
if(user.id == profile.userId){
users.push({user, profile})
}
})
})
session.flash('succes','This is the liste of the users and there profile')
return response.status(200).json(users)
}

public async getAllProducts({response}: HttpContextContract) {
interface ProductAsset{
product: Product;
comments: Array<Comment>;
}
const allComments = await Comment.all()
const allProducts = await Product.all()
const url = await Drive.getUrl("./products")
const products = Array<ProductAsset>()
allProducts.map(product => {
const comments = Array<Comment>()
allComments.map(comment => {
if(comment.productId == product.id){
comments.push(comment)
}
})
product.asset = `${url}/${product.asset}`
products.push({product,comments})
})
return response.status(200).json(products)
}
}
7 changes: 1 addition & 6 deletions app/Controllers/Http/UsersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ export default class UsersController {
return inertia.render("Auth/Register");
}

public async register({
auth,
session,
request,
response,
}: HttpContextContract) {
public async register({auth,session,request,response,}: HttpContextContract) {
const data = await request.validate(RegisterValidator);
const user = await User.create({ ...data, roleId: 1 });

Expand Down
6 changes: 6 additions & 0 deletions start/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Route.get('/register', 'UsersController.registerShow').as('user.register.show')
Route.post('/register', 'UsersController.register').as('user.register')
Route.get('/logout', 'UsersController.logout').as('user.logout')


Route.group(() => {
Route.get('/users', 'ApiController.getAllUsers')
Route.get('/products', 'ApiController.getAllProducts')
}).prefix('/api')

Route.group(() => {
Route.group(() => {
Route.get('/create', 'ProductsController.create').as('product.create')
Expand Down

0 comments on commit e7e2d08

Please sign in to comment.