1+ import touristAttractionService from "../services/touristAttraction.service.js" ;
2+
3+ const getAttractions = async ( req , res ) => {
4+ try {
5+ let attractions = await touristAttractionService . findAllService ( ) ;
6+ if ( ! attractions ) attractions = [ ]
7+ //TODO: colocar as avaliações
8+
9+ res . status ( 200 ) . json ( attractions ) ;
10+ } catch ( error ) {
11+ return res . status ( 500 ) . json ( {
12+ error : error . message ,
13+ } ) ;
14+ }
15+ }
16+
17+ const addAttraction = async ( req , res ) => {
18+ try {
19+
20+ const requiredFields = [
21+ "name" ,
22+ "address" ,
23+ "openingHours" ,
24+ "typeOfAttraction" ,
25+ "description"
26+ ]
27+
28+ for ( const field of requiredFields ) {
29+ if ( ! req . body [ field ] ) {
30+ return res . status ( 400 ) . json ( { error : `Please add the field ${ field } ` } ) ;
31+ }
32+ }
33+
34+ const {
35+ name,
36+ address,
37+ openingHours,
38+ typeOfAttraction,
39+ description
40+ } = req . body
41+
42+ //TODO: testar se o usuário é admin
43+
44+ const attraction = await touristAttractionService . createService ( {
45+ name,
46+ address,
47+ openingHours,
48+ typeOfAttraction,
49+ description
50+ } )
51+
52+ res . status ( 201 ) . json ( {
53+ message : "Tourist Attraction registered successfully" ,
54+ id : attraction . _id
55+ } )
56+
57+ } catch ( error ) {
58+ return res . status ( 500 ) . json ( {
59+ error : error . message ,
60+ } ) ;
61+ }
62+ }
63+
64+ const updateAttraction = async ( req , res ) => {
65+ try {
66+
67+ const id = req . params . id ;
68+
69+ if ( ! id ) {
70+ return res . status ( 400 ) . json ( { error : `Please add the attraction id as a request param` } ) ;
71+ }
72+
73+
74+ const requiredFields = [
75+ "name" ,
76+ "address" ,
77+ "openingHours" ,
78+ "typeOfAttraction" ,
79+ "description"
80+ ]
81+
82+ for ( const field of requiredFields ) {
83+ if ( ! req . body [ field ] ) {
84+ return res . status ( 400 ) . json ( { error : `Please add the field ${ field } ` } ) ;
85+ }
86+ }
87+
88+ const {
89+ name,
90+ address,
91+ openingHours,
92+ typeOfAttraction,
93+ description
94+ } = req . body
95+
96+ //TODO: criar middleware que pega o id do usuário e testa se é admin
97+
98+ const result = await touristAttractionService . updateService ( id , name , address , openingHours , typeOfAttraction , description )
99+
100+ if ( ! result ) {
101+ res . status ( 404 ) . json ( { message : "Tourist Attraction not found" } )
102+ } else {
103+ res . status ( 204 ) . json ( )
104+ }
105+
106+ } catch ( error ) {
107+
108+ return res . status ( 500 ) . json ( {
109+ error : error . message ,
110+ } ) ;
111+
112+ }
113+ }
114+
115+ const deleteAttraction = async ( req , res ) => {
116+ try {
117+
118+ const id = req . params . id ;
119+
120+ if ( ! id ) {
121+ return res . status ( 400 ) . json ( { message : `Please add the attraction id as a request param` } ) ;
122+ }
123+
124+ const result = await touristAttractionService . deleteService ( id )
125+
126+ if ( ! result ) {
127+ res . status ( 404 ) . json ( { error : "Tourist Attraction not found" } )
128+ } else {
129+ res . status ( 204 ) . json ( )
130+ }
131+
132+ } catch ( error ) {
133+
134+ return res . status ( 500 ) . json ( {
135+ error : error . message ,
136+ } ) ;
137+
138+ }
139+ }
140+
141+ export default {
142+ getAttractions,
143+ addAttraction,
144+ deleteAttraction,
145+ updateAttraction
146+ }
0 commit comments