1+ import {
2+ addNewTopic , deleteTopicById , updateTopicById , getTopics ,
3+ } from "#services/topic" ;
4+ import { logger } from "#util" ;
5+
6+ async function addTopic ( req , res ) {
7+ const {
8+ title,
9+ } = req . body ;
10+ try {
11+ // eslint-disable-next-line max-len
12+ const topic = await addNewTopic ( title ) ;
13+ res . json ( { res : `added accreditation ${ topic . name } ` , id : topic . id } ) ;
14+ } catch ( error ) {
15+ logger . error ( "Error while inserting" , error ) ;
16+ res . status ( 500 ) ;
17+ res . json ( { err : "Error while inserting in DB" } ) ;
18+ }
19+ }
20+ async function deleteTopic ( req , res ) {
21+ const { id } = req . params ;
22+ try {
23+ await deleteTopicById ( id ) ;
24+ res . json ( { res : "Topic deleted successfully" } ) ;
25+ } catch ( error ) {
26+ logger . error ( "Error while deleting" , error ) ;
27+ res . status ( 500 ) ;
28+ res . json ( { err : "Error while deleting from DB" } ) ;
29+ }
30+ }
31+
32+ async function updateTopic ( req , res ) {
33+ const { id } = req . params ;
34+ const {
35+ ...data
36+ } = req . body ;
37+
38+ try {
39+ await updateTopicById ( id , data ) ;
40+ res . json ( { res : `${ id } topic updated` } ) ;
41+ } catch ( error ) {
42+ logger . error ( "Error while inserting" , error ) ;
43+ res . status ( 500 ) ;
44+ res . json ( { err : "Error while inserting in DB" } ) ;
45+ }
46+ }
47+
48+ async function showTopic ( req , res ) {
49+ try {
50+ const topic = await getTopics ( req . query ) ;
51+ return res . json ( { res : topic } ) ;
52+ } catch ( error ) {
53+ logger . error ( "Error while fetching" , error ) ;
54+ res . status ( 500 ) ;
55+ return res . json ( { err : "Error while fetching the data" } ) ;
56+ }
57+ }
58+
59+ export default {
60+ addTopic, updateTopic, deleteTopic, showTopic,
61+ } ;
62+
0 commit comments