Skip to content
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

crud de usuários #16

Open
wants to merge 28 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3698fa0
Trigger notification
pedrobuenoxs Nov 8, 2022
3e7b80e
Merge branch 'develop' of github.com:pedrobuenoxs/organikos-backend i…
pedrobuenoxs Nov 8, 2022
1ef1134
fix
pedrobuenoxs Nov 8, 2022
68de619
Merge branch 'develop' into fix-bugs
pedrobuenoxs Nov 8, 2022
3296b01
Merge pull request #10 from pedrobuenoxs/fix-bugs
pedrobuenoxs Nov 8, 2022
12a4b68
fix
pedrobuenoxs Nov 8, 2022
1186a9e
Merge branch 'fix-bugs' of github.com:pedrobuenoxs/organikos-backend …
pedrobuenoxs Nov 8, 2022
8995136
fix
pedrobuenoxs Nov 8, 2022
ef5e9fc
Merge pull request #11 from pedrobuenoxs/fix-bugs
pedrobuenoxs Nov 8, 2022
e233f14
fix
pedrobuenoxs Nov 8, 2022
348bb4b
Merge pull request #12 from pedrobuenoxs/fix-bugs
pedrobuenoxs Nov 8, 2022
1c0f3ed
fix: ssl
pedrobuenoxs Nov 8, 2022
73cec7b
Merge pull request #13 from pedrobuenoxs/fix-bugs
pedrobuenoxs Nov 8, 2022
50d1380
Merge branch 'develop' of github.com:pedrobuenoxs/organikos-backend i…
pedrobuenoxs Nov 8, 2022
8456123
fix: add quantity and return price and quantity as number
pedrobuenoxs Nov 9, 2022
1eedc3f
fix: add quantity and return number as type number
pedrobuenoxs Nov 9, 2022
6fd99bc
Merge pull request #14 from pedrobuenoxs/fix-bugs
pedrobuenoxs Nov 9, 2022
b1c43fc
feat: create get user by id methods
pedrobuenoxs Nov 9, 2022
1b6de21
feat: create get user by id service
pedrobuenoxs Nov 9, 2022
da5ff6f
feat: add edit user methods
pedrobuenoxs Nov 9, 2022
7885f5d
fix: remove sync function
pedrobuenoxs Nov 9, 2022
8b0cd50
feat: add update user logic
pedrobuenoxs Nov 9, 2022
c059257
feat: add delete user by id
pedrobuenoxs Nov 9, 2022
316ba8a
Merge pull request #15 from pedrobuenoxs/fix-bugs
pedrobuenoxs Nov 9, 2022
cf51d90
feat: add post route for get by email/login
pedrobuenoxs Nov 10, 2022
517b324
Merge pull request #16 from pedrobuenoxs/fix-bugs
pedrobuenoxs Nov 10, 2022
58f8dc9
fix
pedrobuenoxs Nov 11, 2022
79547b4
Merge pull request #17 from pedrobuenoxs/fix-bugs
pedrobuenoxs Nov 11, 2022
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
4 changes: 2 additions & 2 deletions src/api/controllers/user/get-by-id.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = class GetUserByIdController {
}
async execute(req, res) {
try {
const { id } = req.params;
const user = await this.user.getById(id);
const { email } = req.body;
const user = await this.user.getByEmail(email);
res.status(200).send(JSON.stringify(user));
} catch (error) {
const errorObj = JSON.parse(error.message);
Expand Down
2 changes: 1 addition & 1 deletion src/api/controllers/user/put-user-by-id.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = class PutUserByIdController {
try {
const { id } = req.params;
const user = await this.user.update(req.body, id);
res.status(204).send(JSON.stringify(user));
res.status(200).send(JSON.stringify(user));
} catch (error) {
const errorObj = JSON.parse(error.message);
if (errorObj.statusCode) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/database/models/order_product.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ const Order_product = sequelize.define('order_product', {
},
});

Order_product.sync();
// Order_product.sync();
module.exports = Order_product;
6 changes: 6 additions & 0 deletions src/api/database/models/product.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ const Product = sequelize.define('product', {
measure: {
type: DataTypes.STRING,
},
quantity: {
type: DataTypes.INTEGER,
},
status: {
type: DataTypes.INTEGER,
},
category: {
type: DataTypes.STRING,
},
url: {
type: DataTypes.STRING,
},
});
// Product.sync();
module.exports = Product;
40 changes: 39 additions & 1 deletion src/api/repositories/address.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ async function findSellerAddress(id) {
},
});
} catch (error) {
console.log(error);
throw new Error(error);
}
}

async function findUserAddress(id) {
try {
return await Address.findOne({
where: {
id_users: id,
},
});
} catch (error) {
throw new Error(error);
}
}

Expand Down Expand Up @@ -46,8 +58,34 @@ async function createUserAddress(address, id) {
}
}

async function updateUserAddress(address, id) {
console.log('id::', id);
try {
return await Address.update(
{
street: address.street,
number: address.number,
complement: address.complement,
city: address.city,
state: address.state,
zip_code: address.zipCode,
country: address.country,
},
{
where: {
id_users: id,
},
}
);
} catch (error) {
throw new Error(error);
}
}

module.exports = {
findSellerAddress,
createSellerAddress,
createUserAddress,
findUserAddress,
updateUserAddress,
};
15 changes: 13 additions & 2 deletions src/api/repositories/product.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ module.exports = class ProductRepository {
constructor() {}
async create(data) {
try {
const { id_seller, name, price, measure, status, category } = data;
const {
id_seller,
name,
price,
measure,
status,
category,
quantity,
} = data;
const product = {
id_seller: id_seller,
name: name,
price,
measure,
quantity,
status,
category,
};
Expand Down Expand Up @@ -63,11 +72,13 @@ module.exports = class ProductRepository {

async update(product) {
try {
const { id, name, price, measure, status, category } = product;
const { id, name, price, measure, status, category, quantity } =
product;
const productObj = {
name: name,
price: price,
measure: measure,
quantity: quantity,
status: status,
category: category,
};
Expand Down
101 changes: 80 additions & 21 deletions src/api/repositories/user.repository.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const User = require('../database/models/user.model');
const { createUserAddress } = require('../repositories/address.repository');
const {
createUserAddress,
findUserAddress,
updateUserAddress,
} = require('../repositories/address.repository');
module.exports = class UserRepository {
constructor() {}
async create(user, address) {
console.log('user', user);
console.log('address', address);
try {
const { userId, name, phone, email, password } = user;
const { street, number, complement, city, state, zipCode } =
address;

if (await this.isRegistered(email))
throw new Error('User already registered');
return User.create({
id: userId,
name,
Expand All @@ -18,16 +21,17 @@ module.exports = class UserRepository {
password,
})
.then(async (user) => {
console.log('user', user);
const address_ = await createUserAddress({
id_users: user.id,
street,
number,
complement,
city,
state,
zipCode,
});
const address_ = await createUserAddress(
{
street,
number,
complement,
city,
state,
zipCode,
},
user.id
);
return { user, address_ };
})
.catch((error) => {
Expand All @@ -37,24 +41,79 @@ module.exports = class UserRepository {
throw new Error(error);
}
}
async update() {
async update(user, address) {
// console.log('user::', user);
try {
throw new Error('no implemented');
const id = user.userId;
const addressParam = address;
return User.update(
{ name: user.name, phone: user.phone, email: user.email },
{
where: {
id: user.userId,
},
}
)
.then(async () => {
await updateUserAddress(addressParam, id);
const { user, address } = await this.getById(id);
return {
user: user.dataValues,
address_: address.dataValues,
};
})
.catch((error) => {
throw new Error(error);
});
} catch (error) {
throw new Error(error);
}
}

async getById() {
try {
throw new Error('get seller by id');
} catch (error) {}
async getByEmail(email) {
return User.findOne({
where: {
email: email,
},
})
.then(async (user) => {
const addressRaw = await findUserAddress(user.id);
const address = {
street: addressRaw.street,
number: addressRaw.number,
complement: addressRaw.complement,
city: addressRaw.city,
state: addressRaw.state,
zipCode: addressRaw.zip_code,
};

return { user, address };
})
.catch((error) => {
throw new Error(error);
});
}
async delete(id) {
try {
return `delete seller by id ${id}`;
const user = await User.findByPk(id);
if (!user) throw new Error('User not found');
await User.destroy({
where: {
id: id,
},
});
} catch (error) {
throw new Error(error);
}
}

async isRegistered(email) {
const user = await User.findOne({
where: {
email: email,
},
});
if (user) return true;
return false;
}
};
6 changes: 3 additions & 3 deletions src/api/routes/user.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ const {

const { postNewProductController } = require('../composer/order.compose');

UserRouter.get('/user/:id', async (req, res) => {
await getUserByIdController.execute(req, res);
});
UserRouter.post('/user', async (req, res) => {
await postNewUserController.execute(req, res);
});
UserRouter.post('/user/login', async (req, res) => {
await getUserByIdController.execute(req, res);
});
UserRouter.put('/user/:id', async (req, res) => {
await putUserByIdController.execute(req, res);
});
Expand Down
8 changes: 6 additions & 2 deletions src/api/services/product/create-product.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ module.exports = class CreateProductService {
}
async create(params, seller_id) {
try {
const { name, price, measure, status, category } = params;
const { name, price, measure, status, category, quantity } = params;

if (!seller_id) throw new Error('Missing seller_id');
if (!name) throw new Error('Missing name');
if (!price) throw new Error('Missing price');
if (!measure) throw new Error('Missing measure');
if (!quantity) throw new Error('Missing quantity');
if (!status) throw new Error('Missing status');
if (!category) throw new Error('Missing category');

Expand All @@ -18,6 +19,7 @@ module.exports = class CreateProductService {
name: name,
price: +price,
measure: measure.toLowerCase(),
quantity: +quantity,
status: status == true ? 1 : 0,
category: category.toLowerCase(),
};
Expand All @@ -28,8 +30,10 @@ module.exports = class CreateProductService {
id: product.id,
seller_id: product.id_seller,
name: product.name,
price: product.price.toString(),
price: +product.price,
measure: product.measure,
quantity: +product.quantity,
url: product.url,
status: product.status == 1 ? true : false,
category: product.category,
};
Expand Down
4 changes: 3 additions & 1 deletion src/api/services/product/get-by-name.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ module.exports = class GetProductByNameService {
id: product.id,
seller_id: product.id_seller,
name: product.name,
price: product.price.toString(),
price: +product.price,
quantity: +product.quantity,
measure: product.measure,
url: product.url,
status: product.status == 1 ? true : false,
category: product.category,
};
Expand Down
4 changes: 3 additions & 1 deletion src/api/services/product/get-by-seller-id.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ module.exports = class GetProductBySellerIdService {
id: product.id,
seller_id: product.id_seller,
name: product.name,
price: product.price.toString(),
price: +product.price,
quantity: +product.quantity,
measure: product.measure,
url: product.url,
status: product.status == 1 ? true : false,
category: product.category,
};
Expand Down
4 changes: 3 additions & 1 deletion src/api/services/product/get-by-tag.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ module.exports = class GetProductByTagService {
id: product.id,
seller_id: product.id_seller,
name: product.name,
price: product.price.toString(),
price: +product.price,
quantity: +product.quantity,
measure: product.measure,
url: product.url,
status: product.status == 1 ? true : false,
category: product.category,
};
Expand Down
Loading