Skip to content

Commit

Permalink
Merge branch 'develop' into editCourse
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeMC90 authored Jan 13, 2023
2 parents 29a0444 + 982ec55 commit 039a007
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 11 deletions.
51 changes: 47 additions & 4 deletions API/src/Controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ const getAllCourses = async (req, res) => {
rating: c.rating,
image: c.image,
active: c.active,
enabled: c.enabled,
difficulty: c.difficulty,
createdAt: c.createdAt,
updatedAt: c.updatedAt,
Expand All @@ -200,7 +201,8 @@ const getAllCourses = async (req, res) => {
});

if (courses.length > 0) {
return res.status(200).send(courses);
const coursesActive = courses.filter((u) => u.active === true);
return res.status(200).send(coursesActive);
}
res.status(404).send({ message: "No se encontraron cursos" });
} catch (error) {
Expand Down Expand Up @@ -242,10 +244,10 @@ const postReview = async (req, res) => {
const getUsers = async (req, res) => {
try {
const users = await Users.findAll();
res.json(users);
// console.log("user api", users);
const userEnable = users.filter((u) => u.active === true);
res.json(userEnable);
} catch (error) {
res.status(401).json(error.name);
res.status(401).json({ message: error.message });
}
};
const createUser = async (req, res) => {
Expand Down Expand Up @@ -765,6 +767,45 @@ const editCourse = async (req, res) => {
} catch (error) {
res.status(404).send({ message: error.message });
console.log('Error edit :', error.message)

const disableCourse = async (req, res) => {
const { id } = req.params;

try {
const course = await Courses.findByPk(id);
if (course) {
await Courses.update({ enabled: !course.enabled }, { where: { id: id } });
res
.status(200)
.json({ message: `estado admin del curso ${!course.enabled}` });
} else {
res.status(404).json({ message: "Curso no encontrado" });
}
} catch (error) {
res.status(400).json({ message: error.message });
}

};

const deleteCourse = async (req, res) => {
const { id } = req.params;

try {
if (id) {
const course = await Courses.findByPk(id);

if (course) {
await Courses.update({ active: false }, { where: { id: id } });
await Courses.update({ enabled: false }, { where: { id: id } });
res.status(200).json({ message: "el curso ha sido eliminado" });
} else {
res.status(404).json({ message: "Curso no encontrado" });
}
} else {
res.status(404).json({ message: "Curso no encontrado" });
}
} catch (error) {
res.status(400).json({ message: error.message });
}
};

Expand Down Expand Up @@ -794,4 +835,6 @@ module.exports = {
deleteUser,
getUserEmail,
editCourse,
disableCourse,
deleteCourse
};
2 changes: 1 addition & 1 deletion API/src/Routes/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { postPayment, postInformationBuyer, getOrders, linkMail } = require('../C

router
.post('/information', postInformationBuyer)
.post('/payment', postPayment,linkMail ) // cuidado con excederse con los envios de mail
.post('/payment', postPayment) // ,linkMail cuidado con excederse con los envios de mail
.get('/orders', getOrders) // ruta de prueba para que vean como quedaron relacionadas las ordenes

module.exports = router;
5 changes: 4 additions & 1 deletion API/src/Routes/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {
getCourseById,
postReview,
editCourse
disableCourse,
deleteCourse
} = require('../Controllers/index.js')

router
Expand All @@ -15,6 +17,7 @@ router
.post('/', postCourse)
.post('/review', postReview)
.put('/', editCourse)

.put('/disable/:id', disableCourse)
.put('/delete/:id', deleteCourse)

module.exports = router;
2 changes: 1 addition & 1 deletion Client/src/Component/LogoutButton/LogoutButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Menu, MenuButton, MenuList, WrapItem, Avatar } from "@chakra-ui/react";
import Profile from "../Profile/Profile";
import axios from "axios";
import { useDispatch } from "react-redux";
import { setUserLocalStore, setLoguinLocalStore, getUserLocalStore } from "../../Redux/actions/index";
import { setUserLocalStore, setLoguinLocalStore, getUserLocalStore, getUsers } from "../../Redux/actions/index";

const LogoutButton = () => {
const { logout, user } = useAuth0();
Expand Down
69 changes: 65 additions & 4 deletions Client/src/Component/admin/coursesAdmin/coursesAdmin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import {
Box,
IconButton,
Switch,
Table,
TableCaption,
TableContainer,
Expand All @@ -13,8 +15,10 @@ import {
Tr,
} from "@chakra-ui/react";
import { getCourses, getDetail } from "../../../Redux/actions/index";
import {EditIcon} from '@chakra-ui/icons';
import {DeleteIcon, EditIcon} from '@chakra-ui/icons';
import SearchAdmin from "./searchAdmin/searchAdmin";
import axios from "axios";
import Swal from "sweetalert2";
import { useHistory, Link} from 'react-router-dom';

function CoursesAdmin() {
Expand All @@ -36,6 +40,31 @@ function CoursesAdmin() {
history.push(`/editcourse/${id}`)
}

const handleDisable = async (id) => {
try {
await axios.put(`http://localhost:3001/courses/disable/${id}`);
dispatch(getCourses(""));
} catch (error) {
console.log(error);
}
};

const handleDelete = async (id) => {
try {
await axios.put(`http://localhost:3001/courses/delete/${id}`);
dispatch(getCourses(""));
Swal.fire({
position: 'center',
icon: 'success',
title: 'Course Deleted',
showConfirmButton: false,
timer: 1500
})
} catch (error) {
console.log(error);
}
};

return (
<>
<SearchAdmin/>
Expand All @@ -49,6 +78,8 @@ function CoursesAdmin() {
<Th>Rating</Th>
<Th>Categories</Th>
<Th>Price</Th>
<Th>Enable</Th>
<Th>Edit</Th>
<Th></Th>
</Tr>
</Thead>
Expand All @@ -64,10 +95,18 @@ function CoursesAdmin() {
{/* visitas */}
<Td pl="3%">{value.rating}</Td>
{/* categoria */}
<Td>{value.category}</Td>
<Td>{value.categories[0]}</Td>
{/* precio */}
<Td>{`$${value.price} USD`}</Td>

{/* admin*/}
<Td>
<Box>
<Switch
isChecked={value.enabled}
onChange={()=>handleDisable(value.id)}
/>
</Box>
</Td>
<Td>
{/* <Link to={`/editcourse/${value.id}`}> */}

Expand Down Expand Up @@ -95,7 +134,29 @@ function CoursesAdmin() {
{/* </Link> */}

</Td>

<Td>
{/* boton para eliminar */}
<IconButton
onClick={()=>handleDelete(value.id)}
flex={1}
fontSize={"sm"}
rounded={"full"}
maxW="10px"
bg={"blue.400"}
color={"white"}
boxShadow={
"0px 1px 25px -5px rgb(66 153 225 / 48%), 0 10px 10px -5px rgb(66 153 225 / 43%)"
}
_hover={{
bg: "blue.500",
}}
_focus={{
bg: "blue.500",
}}
aria-label="Search database"
icon={<DeleteIcon />}
/>
</Td>
</Tr>
);
})}
Expand Down

0 comments on commit 039a007

Please sign in to comment.