Skip to content

Commit

Permalink
Merge pull request #14 from CampusTecNode/feature/adding-image-fields…
Browse files Browse the repository at this point in the history
…-to-users-and-categories

Add ImageURL field to Category and User models
  • Loading branch information
David-PX authored Oct 14, 2024
2 parents bd707fc + 2736e4b commit 7f66b9f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 46 deletions.
8 changes: 4 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const port = process.env.PORT || 3000;
async function initializeApp() {
try {
// Sincroniza todos los modelos con la base de datos
await sequelize.sync();
await sequelize.sync({ force: true });
console.log('Tablas creadas exitosamente.');

// if (process.env.NODE_ENV === 'development') {
// await seedDatabase();
// }
if (process.env.NODE_ENV === 'development') {
await seedDatabase();
}

app.listen(port, () => {
console.log(`Servidor escuchando en el puerto ${port}`);
Expand Down
10 changes: 9 additions & 1 deletion src/auth/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ const loginUser = async (req, res) => {
{ expiresIn: process.env.JWT_EXPIRATION }
);

res.json({ token: token, userID: user.id, stripeid: 'cus_R0u5BxAeykbbZm' });
res.json({ token: token,
userID: user.id,
username: user.username,
name: user.name,
lastname: user.lastname,
email: user.email,
phone: user.phone,
imageURL: user.imageURL,
customerID: 'cus_R0u5BxAeykbbZm' });
} catch (error) {
res.status(500).json({ message: 'Error logging in', error: error.message });
}
Expand Down
11 changes: 6 additions & 5 deletions src/categories/categoriesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Get = async (req, res) => {
const { userID } = req.query;

const categories = await Categories.findAll({
attributes: ['ID', 'Name', 'Description', 'CreatedAt', 'CreatedBy', 'UpdatedAt', 'UpdatedBy'],
attributes: ['ID', 'Name', 'Description', 'ImageURL', 'CreatedAt', 'CreatedBy', 'UpdatedAt', 'UpdatedBy'],
include: [
{
model: Products,
Expand Down Expand Up @@ -58,7 +58,7 @@ const GetByID = async (req, res) => {
try {
const { id } = req.params;
const category = await Categories.findByPk(id, {
attributes: ['ID', 'Name', 'Description', 'CreatedAt', 'CreatedBy', 'UpdatedAt', 'UpdatedBy'],
attributes: ['ID', 'Name', 'Description', 'ImageURL', 'CreatedAt', 'CreatedBy', 'UpdatedAt', 'UpdatedBy'],
where: { DeletedAt: null },
});

Expand All @@ -77,8 +77,8 @@ const Create = async (req, res) => {
/* #swagger.tags = ['Categories']
#swagger.description = 'Create one new Category' */
try {
const { Name, Description } = req.body;
const newCategory = await Categories.create({ Name, Description });
const { Name, Description, ImageURL } = req.body;
const newCategory = await Categories.create({ Name, Description, ImageURL });
return res.status(201).json(newCategory);
} catch (error) {
console.error('Error creating category:', error);
Expand All @@ -91,7 +91,7 @@ const Update = async (req, res) => {
#swagger.description = 'Update one Category' */
try {
const { id } = req.params;
const { Name, Description } = req.body;
const { Name, Description, ImageURL } = req.body;

const category = await Categories.findByPk(id);
if (!category) {
Expand All @@ -100,6 +100,7 @@ const Update = async (req, res) => {

category.Name = Name || category.Name;
category.Description = Description || category.Description;
category.ImageURL = ImageURL || category.ImageURL;
await category.save();

return res.json(category);
Expand Down
4 changes: 4 additions & 0 deletions src/data/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const Categories = sequelize.define('Categories', {
type: DataTypes.STRING,
allowNull: true,
},
ImageURL: {
type: DataTypes.STRING,
allowNull: true,
},
CreatedAt:{
type: DataTypes.DATE,
allowNull: false,
Expand Down
12 changes: 12 additions & 0 deletions src/data/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const Users = sequelize.define('Users', {
allowNull: false,
unique: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
lastname: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
Expand Down Expand Up @@ -42,6 +50,10 @@ const Users = sequelize.define('Users', {
type: DataTypes.STRING,
allowNull: false,
},
ImageURL: {
type: DataTypes.STRING,
allowNull: true,
},
CreatedAt:{
type: DataTypes.DATE,
allowNull: false,
Expand Down
Loading

0 comments on commit 7f66b9f

Please sign in to comment.