Skip to content

Commit 03ee273

Browse files
authored
Merge pull request #137 from alkemyTech/realese_v_1.3.0
Realese v 1.3.0
2 parents b49dd20 + 3c4111b commit 03ee273

26 files changed

+1200
-82
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,37 @@ npx sequelize-cli db:seed:all
2626
``` bash
2727
npm start
2828
```
29+
30+
## TEST USERS
31+
32+
# ADMIN USERS
33+
34+
| FirstName | lastName | email | password |
35+
|----------------|----------|------------------|----------|
36+
| usuarioAdmin1 | Demo1 | test1@admin.com | admin |
37+
| usuarioAdmin2 | Demo2 | test2@admin.com | admin |
38+
| usuarioAdmin3 | Demo3 | test3@admin.com | admin |
39+
| usuarioAdmin4 | Demo4 | test4@admin.com | admin |
40+
| usuarioAdmin5 | Demo5 | test5@admin.com | admin |
41+
| usuarioAdmin6 | Demo6 | test6@admin.com | admin |
42+
| usuarioAdmin7 | Demo7 | test7@admin.com | admin |
43+
| usuarioAdmin8 | Demo8 | test8@admin.com | admin |
44+
| usuarioAdmin9 | Demo9 | test9@admin.com | admin |
45+
| usuarioAdmin10 | Demo10 | test10@admin.com | admin |
46+
47+
# STANDAR USERS
48+
49+
| FirstName | lastName | email | password |
50+
|----------------|----------|------------------|----------|
51+
| usuarioStandar1 | Demo1 | test1@user.com | user |
52+
| usuarioStandar2 | Demo2 | test2@user.com | user |
53+
| usuarioStandar3 | Demo3 | test3@user.com | user |
54+
| usuarioStandar4 | Demo4 | test4@user.com | user |
55+
| usuarioStandar5 | Demo5 | test5@user.com | user |
56+
| usuarioStandar6 | Demo6 | test6@user.com | user |
57+
| usuarioStandar7 | Demo7 | test7@user.com | user |
58+
| usuarioStandar8 | Demo8 | test8@user.com | user |
59+
| usuarioStandar9 | Demo9 | test9@user.com | user |
60+
| usuarioStandar10 | Demo10 | test10usern.com | user |
61+
62+

controllers/commentsController.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
const { Comments } = require('../models');
1+
const CommentDao = require('../dao/comments');
22

33
class CommentsControllers {
4+
static async getComments(req, res) {
5+
try {
6+
const { id } = req.params;
7+
const comments = await CommentDao.listCommentsByPost(id);
8+
res.status(200).json(comments);
9+
} catch (error) {
10+
res.status(400).json('Error. The query could not be performed.');
11+
}
12+
}
13+
static async modifyComment(req, res) {
14+
try {
15+
const { id } = req.params;
16+
const { body } = req.body;
417

18+
const resultUpdate = await CommentDao.updateComment({ id }, { body });
19+
20+
return res.status(200).json({
21+
resultUpdate,
22+
});
23+
} catch (error) {
24+
return res.status(500).json({
25+
msg: `Error while updating comment`,
26+
});
27+
}
28+
}
529
}
630

7-
module.exports = CommentsControllers;
31+
module.exports = CommentsControllers;

controllers/member.controller.js

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ const MemberDao = require('../dao/member');
33

44
class MemberController {
55
static async getMembers(req, res) {
6-
const attributes = ['name', 'image'];
6+
const attributes = [
7+
'name',
8+
'facebookUrl',
9+
'instagramUrl',
10+
'linkedinUrl',
11+
'image',
12+
'description',
13+
];
714

815
try {
916
const response = await MemberDao.getMembers(attributes);
10-
1117
return res.status(200).json(response);
1218
} catch (error) {
1319
return res.status(500).json({
14-
error,
15-
msg: 'error in db',
20+
msg: 'error while searching members in db',
1621
});
1722
}
1823
}
@@ -52,6 +57,50 @@ class MemberController {
5257
res.status(400).json(error);
5358
}
5459
}
60+
static async updateMember(req, res) {
61+
try {
62+
const {
63+
name,
64+
facebookUrl,
65+
instagramUrl,
66+
linkedinUrl,
67+
image,
68+
description,
69+
} = req.body;
70+
const { id } = req.params;
71+
const resultUpdate = await MemberDao.updateMember(
72+
{ id },
73+
{
74+
name,
75+
facebookUrl,
76+
instagramUrl,
77+
linkedinUrl,
78+
image,
79+
description,
80+
}
81+
);
82+
res.status(200).json(resultUpdate);
83+
} catch (error) {
84+
res.status(400).json(error);
85+
}
86+
}
87+
88+
static async getMemberById(req, res) {
89+
const attributes = ['name', 'image'];
90+
const { id } = req.params;
91+
92+
let member;
93+
94+
try {
95+
member = await MemberDao.getMembersById(id, attributes);
96+
} catch (error) {
97+
res.status(400).json(error);
98+
}
99+
100+
member.length === 0
101+
? res.status(404).json({ msg: 'Could not find member' })
102+
: res.status(200).json(member);
103+
}
55104
}
56105

57106
module.exports = MemberController;

controllers/slider.controller.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const SlidesDao = require('../dao/slide');
2+
const UploadFiles = require('../helpers/UploadFiles.js');
3+
const FileManager = require('../helpers/FileManager.js');
4+
25
class SlidersController {
36
static async getDetails(req, res) {
47
const { id } = req.params;
@@ -49,6 +52,40 @@ class SlidersController {
4952
});
5053
}
5154
}
55+
56+
static async postSlide(req, res) {
57+
const { base64Image, text, name } = req.body;
58+
let { order } = req.body;
59+
let decodedImage;
60+
61+
if (!name) return res.status(400).json({
62+
msg: 'A name is required',
63+
});
64+
65+
try {
66+
decodedImage = await UploadFiles.decodeImage(base64Image, name)
67+
} catch(error) {
68+
res.status(400).json({ msg: 'Could not decode image' })
69+
}
70+
71+
if (!order) {
72+
order = (await SlidesDao.sortSlides('order', 'DESC')).map(o => o.order)[0] + 1
73+
}
74+
75+
try {
76+
const newSlide = await SlidesDao.createSlide(
77+
{
78+
imageUrl: decodedImage,
79+
order,
80+
text,
81+
},
82+
[ 'imageUrl', 'order', 'text' ]
83+
);
84+
res.status(200).json({ msg: 'Slide created successfully' })
85+
} catch (error) {
86+
res.status(400).json({ msg: 'Could not create a slide' })
87+
}
88+
}
5289
}
5390

5491
module.exports = SlidersController;

controllers/testimonial.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const TestimonialDao = require('../dao/testimonials');
22
const { Testimonial } = require('../models');
3+
const Pagination = require('../helpers/pagination');
34
class TestimonialController {
45
static async updateTestimonials(req, res) {
56
const { id } = req.params;
@@ -42,23 +43,65 @@ class TestimonialController {
4243
}
4344
}
4445
static async getTestimonials(req, res) {
46+
if (!req.query.page) {
47+
try {
48+
const testimonials = await TestimonialDao.getTestimonials([
49+
'name',
50+
'image',
51+
'content',
52+
]);
53+
54+
if (!testimonials) {
55+
return res.status(404).json({
56+
msg: 'There is no testimonials',
57+
});
58+
}
59+
60+
return res.json({
61+
testimonials,
62+
});
63+
} catch (error) {
64+
return res.status(500).json({
65+
msg: 'Error while searching testimonials ',
66+
});
67+
}
68+
}
69+
const pagination = new Pagination(req, res);
70+
const { page, size } = pagination.getPaginationParams(req, res);
71+
4572
try {
46-
const testimonials = await Testimonial.findAll({
73+
const testimonials = await Testimonial.findAndCountAll({
4774
attributes: ['name', 'image', 'content'],
75+
limit: size,
76+
offset: page * size,
4877
});
4978

50-
if (!testimonials) {
79+
let totalPages = pagination.getNumberOfTotalPages(
80+
testimonials.count,
81+
size
82+
);
83+
84+
if (testimonials.count < 1) {
5185
return res.status(404).json({
5286
msg: 'There is no testimonials',
5387
});
5488
}
5589

90+
const { nextPage, previousPage } = pagination.getNextAndPreviousPage(
91+
page,
92+
size,
93+
totalPages
94+
);
95+
5696
return res.status(200).json({
57-
testimonials,
97+
content: testimonials.rows,
98+
totalPages,
99+
nextPage,
100+
previousPage,
58101
});
59102
} catch (error) {
60103
return res.status(500).json({
61-
msg: 'error while searching in db',
104+
msg: 'Error while searching testimonials in db',
62105
});
63106
}
64107
}

dao/comments.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { Comment } = require('../models');
2+
3+
class CommentDao {
4+
static async listCommentsByPost(id) {
5+
try {
6+
const comments = await Comment.findAll({
7+
where: {
8+
post_id: id,
9+
},
10+
attributes: ['body'],
11+
});
12+
return comments.length ? comments : 'No matches found';
13+
} catch (error) {
14+
throw new Error('Error. The query could not be performed.');
15+
}
16+
}
17+
static async updateComment(where, data) {
18+
try {
19+
const commentUpdated = await Comment.update(data, {
20+
where: where,
21+
});
22+
23+
if (commentUpdated[0] < 1) {
24+
return 'Comment not found';
25+
}
26+
27+
return 'Comment updated succesfully';
28+
} catch (error) {
29+
throw new Error('Error Comment not updated');
30+
}
31+
}
32+
}
33+
34+
module.exports = CommentDao;

dao/member.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,33 @@ class MemberDao {
5050
throw new Error('Error. Member not created.');
5151
}
5252
}
53+
static async updateMember(where, data) {
54+
try {
55+
const memberUpdated = await Member.update(data, {
56+
where: where,
57+
});
58+
return memberUpdated[0] === 0
59+
? 'Member not found.'
60+
: 'Member updated successfully';
61+
} catch (error) {
62+
throw new Error('Error. Member not updated.');
63+
}
64+
}
65+
66+
static async getMembersById(id, attributes) {
67+
try {
68+
const member = await Member.findAll({
69+
where: {
70+
id,
71+
},
72+
attributes,
73+
});
74+
75+
return member;
76+
} catch (error) {
77+
throw new Error('Error. Member not found');
78+
}
79+
}
5380
}
5481

5582
module.exports = MemberDao;

dao/slide.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,39 @@ class SlidesDap {
5252
} catch (error) {
5353
throw new Error(error);
5454
}
55+
}
56+
/**
57+
* Asynchronously and extensible function return a Slide from the database.
58+
* @param {Object} attributes
59+
* @param {Array} fields - limit what attributes can be setted by user
60+
*
61+
* @returns {Object}
62+
*/
63+
static async createSlide(attributes, direction) {
64+
try {
65+
const newSlide = await Slide.create(attributes, direction);
66+
return newSlide;
67+
} catch (error) {
68+
throw Error(error)
69+
}
70+
}
71+
/**
72+
* Asynchronously and extensible function return Slide table sort by choosen direction.
73+
* @param {string} column - table to be sorted
74+
* @param {string} direction - direction that column will be sort
75+
* EJ: `sortSlides('slides', 'ASC')`
76+
*
77+
* @returns {Array}
78+
*/
79+
static async sortSlides(column, direction) {
80+
try {
81+
const allSlides = await Slide.findAll({
82+
order: [ [column, direction] ]
83+
});
84+
return allSlides;
85+
} catch (error) {
86+
return error
87+
}
5588
}
5689
}
5790

0 commit comments

Comments
 (0)