Skip to content
Merged
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
30 changes: 27 additions & 3 deletions app/controllers/construction_sites_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,33 @@ export default class ConstructionSitesController {
* @param params: id of the construction site
* @returns list with the construction site with the given id
*/
show({ params }: HttpContext) {
let constructionSite = ConstructionSite.findById(params.id)
return constructionSite
async show({ request, params, response }: HttpContext) {
const user = request.user
let userId = user?._id
// Recupera tutti i cantieri prendendo la query come parametro sulla via e sul nome LIKE
const constructionSites = await ConstructionSite.find({
$or: [
{ street: { $regex: params.query, $options: 'i' } },
{ name: { $regex: params.query, $options: 'i' } },
],
})
if (userId) {
// Recupera le iscrizioni dell'utente
const subscriptions = await Subscription.find({ user_id: userId })
// Estrai gli ID dei cantieri ai quali è iscritto
const subscribedSites = new Set(
subscriptions.map((sub) => sub.construction_site_id.toString())
)
// Aggiungi la proprietà is_subscribed ai cantieri
const enrichedSites = constructionSites.map((site) => ({
...site.toObject(),
is_subscribed: subscribedSites.has(site._id.toString()),
}))

return response.ok({ user, constructionSites: enrichedSites })
}
// Se l'utente non è autenticato, restituisci i cantieri senza is_subscribed
return response.ok({ user, constructionSites })
}

/**
Expand Down
2 changes: 1 addition & 1 deletion start/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ router
router
.group(() => {
router.get('/', [ConstructionSitesController, 'index']).use(middleware.dashboardView()) // List all construction sites
router.get('/:id', [ConstructionSitesController, 'show']).use(middleware.dashboardView()) // Get a specific construction site
router.get('/:query', [ConstructionSitesController, 'show']).use(middleware.dashboardView()) // Get a specific construction site
router.post('/', [ConstructionSitesController, 'store']).use(middleware.jwtAuth()) // Create a new construction site
router.put('/:id', [ConstructionSitesController, 'update']).use(middleware.jwtAuth()) // Update an existing construction site
router.delete('/:id', [ConstructionSitesController, 'destroy']).use(middleware.jwtAuth()) // Delete a construction site
Expand Down