simplified create item. Restructured profile. Simplified availability

This commit is contained in:
jackiettran
2025-08-19 17:28:22 -04:00
parent 99eae4774e
commit 66dc187295
11 changed files with 1317 additions and 872 deletions

View File

@@ -1,34 +1,36 @@
const express = require('express');
const { Op } = require('sequelize');
const { Item, User, Rental } = require('../models'); // Import from models/index.js to get models with associations
const { authenticateToken } = require('../middleware/auth');
const express = require("express");
const { Op } = require("sequelize");
const { Item, User, Rental } = require("../models"); // Import from models/index.js to get models with associations
const { authenticateToken } = require("../middleware/auth");
const router = express.Router();
router.get('/', async (req, res) => {
router.get("/", async (req, res) => {
try {
const {
isPortable,
minPrice,
maxPrice,
location,
city,
zipCode,
search,
page = 1,
limit = 20
limit = 20,
} = req.query;
const where = {};
if (isPortable !== undefined) where.isPortable = isPortable === 'true';
if (minPrice || maxPrice) {
where.pricePerDay = {};
if (minPrice) where.pricePerDay[Op.gte] = minPrice;
if (maxPrice) where.pricePerDay[Op.lte] = maxPrice;
}
if (location) where.location = { [Op.iLike]: `%${location}%` };
if (city) where.city = { [Op.iLike]: `%${city}%` };
if (zipCode) where.zipCode = { [Op.iLike]: `%${zipCode}%` };
if (search) {
where[Op.or] = [
{ name: { [Op.iLike]: `%${search}%` } },
{ description: { [Op.iLike]: `%${search}%` } }
{ description: { [Op.iLike]: `%${search}%` } },
];
}
@@ -36,37 +38,43 @@ router.get('/', async (req, res) => {
const { count, rows } = await Item.findAndCountAll({
where,
include: [{ model: User, as: 'owner', attributes: ['id', 'username', 'firstName', 'lastName'] }],
include: [
{
model: User,
as: "owner",
attributes: ["id", "username", "firstName", "lastName"],
},
],
limit: parseInt(limit),
offset: parseInt(offset),
order: [['createdAt', 'DESC']]
order: [["createdAt", "DESC"]],
});
res.json({
items: rows,
totalPages: Math.ceil(count / limit),
currentPage: parseInt(page),
totalItems: count
totalItems: count,
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
router.get('/recommendations', authenticateToken, async (req, res) => {
router.get("/recommendations", authenticateToken, async (req, res) => {
try {
const userRentals = await Rental.findAll({
where: { renterId: req.user.id },
include: [{ model: Item, as: 'item' }]
include: [{ model: Item, as: "item" }],
});
// For now, just return random available items as recommendations
const recommendations = await Item.findAll({
where: {
availability: true
availability: true,
},
limit: 10,
order: [['createdAt', 'DESC']]
order: [["createdAt", "DESC"]],
});
res.json(recommendations);
@@ -75,14 +83,20 @@ router.get('/recommendations', authenticateToken, async (req, res) => {
}
});
router.get('/:id', async (req, res) => {
router.get("/:id", async (req, res) => {
try {
const item = await Item.findByPk(req.params.id, {
include: [{ model: User, as: 'owner', attributes: ['id', 'username', 'firstName', 'lastName'] }]
include: [
{
model: User,
as: "owner",
attributes: ["id", "username", "firstName", "lastName"],
},
],
});
if (!item) {
return res.status(404).json({ error: 'Item not found' });
return res.status(404).json({ error: "Item not found" });
}
res.json(item);
@@ -91,15 +105,21 @@ router.get('/:id', async (req, res) => {
}
});
router.post('/', authenticateToken, async (req, res) => {
router.post("/", authenticateToken, async (req, res) => {
try {
const item = await Item.create({
...req.body,
ownerId: req.user.id
ownerId: req.user.id,
});
const itemWithOwner = await Item.findByPk(item.id, {
include: [{ model: User, as: 'owner', attributes: ['id', 'username', 'firstName', 'lastName'] }]
include: [
{
model: User,
as: "owner",
attributes: ["id", "username", "firstName", "lastName"],
},
],
});
res.status(201).json(itemWithOwner);
@@ -108,22 +128,28 @@ router.post('/', authenticateToken, async (req, res) => {
}
});
router.put('/:id', authenticateToken, async (req, res) => {
router.put("/:id", authenticateToken, async (req, res) => {
try {
const item = await Item.findByPk(req.params.id);
if (!item) {
return res.status(404).json({ error: 'Item not found' });
return res.status(404).json({ error: "Item not found" });
}
if (item.ownerId !== req.user.id) {
return res.status(403).json({ error: 'Unauthorized' });
return res.status(403).json({ error: "Unauthorized" });
}
await item.update(req.body);
const updatedItem = await Item.findByPk(item.id, {
include: [{ model: User, as: 'owner', attributes: ['id', 'username', 'firstName', 'lastName'] }]
include: [
{
model: User,
as: "owner",
attributes: ["id", "username", "firstName", "lastName"],
},
],
});
res.json(updatedItem);
@@ -132,16 +158,16 @@ router.put('/:id', authenticateToken, async (req, res) => {
}
});
router.delete('/:id', authenticateToken, async (req, res) => {
router.delete("/:id", authenticateToken, async (req, res) => {
try {
const item = await Item.findByPk(req.params.id);
if (!item) {
return res.status(404).json({ error: 'Item not found' });
return res.status(404).json({ error: "Item not found" });
}
if (item.ownerId !== req.user.id) {
return res.status(403).json({ error: 'Unauthorized' });
return res.status(403).json({ error: "Unauthorized" });
}
await item.destroy();
@@ -151,4 +177,4 @@ router.delete('/:id', authenticateToken, async (req, res) => {
}
});
module.exports = router;
module.exports = router;