@@ -9,120 +9,154 @@ const Url = require("../../models/url");
9
9
const User = require ( "../../models/user" ) ;
10
10
11
11
router . get ( "/" , middleAuth , async ( req , res ) => {
12
- try {
13
- const user = await User . findById ( req . user . id )
14
- . populate ( "shortenedUrl" )
15
- . exec ( ) ;
16
- if ( ! user ) {
17
- return res . status ( 400 ) . json ( { msg : "User does not exist" } ) ;
18
- } else {
19
- return res
20
- . status ( 200 )
21
- . json ( { msg : "User found" , url : user . shortenedUrl } ) ;
22
- }
23
- } catch ( err ) {
24
- console . log ( err ) ;
25
- res . status ( 500 ) . send ( { msg : "Internal Server Error" } ) ;
26
- }
12
+ try {
13
+ const user = await User . findById ( req . user . id )
14
+ . populate ( "shortenedUrl" )
15
+ . exec ( ) ;
16
+ if ( ! user ) {
17
+ return res . status ( 400 ) . json ( { msg : "User does not exist" } ) ;
18
+ } else {
19
+ return res
20
+ . status ( 200 )
21
+ . json ( { msg : "User found" , url : user . shortenedUrl } ) ;
22
+ }
23
+ } catch ( err ) {
24
+ console . log ( err ) ;
25
+ res . status ( 500 ) . send ( { msg : "Internal Server Error" } ) ;
26
+ }
27
+ } ) ;
28
+
29
+ router . get ( "/code" , middleAuth , async ( req , res ) => {
30
+ const { code } = req . query ;
31
+
32
+ let urlCode ;
33
+ console . log ( req ) ;
34
+
35
+ if ( code ) {
36
+ urlCode = code ;
37
+
38
+ if ( await Url . findOne ( { code : urlCode } ) ) {
39
+ return res . status ( 409 ) . send ( {
40
+ present : true ,
41
+ msg : "This code is already in use, try another code" ,
42
+ } ) ;
43
+ } else
44
+ return res
45
+ . status ( 200 )
46
+ . send ( { present : false , msg : `${ code } is available` } ) ;
47
+ } else
48
+ return res
49
+ . status ( 409 )
50
+ . send ( { present : false , msg : "Enter a valid code to search" } ) ;
27
51
} ) ;
28
52
29
53
router . post ( "/compress" , middleAuth , async ( req , res ) => {
30
- const { longUrl, lastDate } = req . body ;
31
-
32
- // const baseUrl = "http://localhost:5000";
33
- const baseUrl = process . env . BASE_URL ;
34
- if ( ! validUrl . isUri ( baseUrl ) ) {
35
- return res . status ( 400 ) . send ( { msg : "Invalid Base Url" } ) ;
36
- }
37
-
38
- const urlCode = shortId . generate ( ) ;
39
-
40
- if ( validUrl . isUri ( longUrl ) ) {
41
- try {
42
- let url = await Url . findOne ( { longUrl : longUrl } ) ;
43
-
44
- if ( url ) {
45
- res
46
- . status ( 400 )
47
- . send ( { msg : "Compressed URL already exists" , url : url . shortUrl } ) ;
48
- } else {
49
- const shortUrl = baseUrl + "/" + urlCode ;
50
-
51
- url = new Url ( {
52
- longUrl,
53
- shortUrl,
54
- urlCode,
55
- lastDate : lastDate ,
56
- code : urlCode ,
57
- creator : req . user . id ,
58
- } ) ;
59
- const newUrl = await url . save ( ) ;
60
-
61
- const user = await User . findById ( req . user . id ) ;
62
- await user . shortenedUrl . push ( newUrl . _id ) ;
63
- await user . save ( ) ;
64
-
65
- res
66
- . status ( 200 )
67
- . json ( { msg : "URL Compressed Successfully" , url : newUrl } ) ;
68
- }
69
- } catch ( err ) {
70
- console . log ( err ) ;
71
- res . status ( 500 ) . send ( { msg : "Internal Server Error" } ) ;
72
- }
73
- } else {
74
- res . status ( 401 ) . send ( { msg : "Invalid Long URL" } ) ;
75
- }
54
+ const { longUrl, lastDate, code } = req . body ;
55
+
56
+ const baseUrl = process . env . BASE_URL ;
57
+ if ( ! validUrl . isUri ( baseUrl ) ) {
58
+ return res . status ( 400 ) . send ( { msg : "Invalid Base Url" } ) ;
59
+ }
60
+
61
+ let urlCode ;
62
+ if ( code ) {
63
+ urlCode = code ;
64
+
65
+ if ( await Url . findOne ( { code : urlCode } ) ) {
66
+ return res
67
+ . status ( 409 )
68
+ . send ( { msg : "This code is already in use, try another code" } ) ;
69
+ }
70
+ } else {
71
+ urlCode = shortId . generate ( ) ;
72
+ }
73
+
74
+ if ( validUrl . isUri ( longUrl ) ) {
75
+ try {
76
+ let url = await Url . findOne ( { longUrl : longUrl } ) ;
77
+
78
+ if ( url ) {
79
+ res
80
+ . status ( 400 )
81
+ . send ( { msg : "Compressed URL already exists" , url : url . shortUrl } ) ;
82
+ } else {
83
+ const shortUrl = baseUrl + "/" + urlCode ;
84
+
85
+ url = new Url ( {
86
+ longUrl,
87
+ shortUrl,
88
+ lastDate : lastDate ,
89
+ code : urlCode ,
90
+ creator : req . user . id ,
91
+ status : 1 ,
92
+ } ) ;
93
+ const newUrl = await url . save ( ) ;
94
+
95
+ const user = await User . findById ( req . user . id ) ;
96
+ await user . shortenedUrl . push ( newUrl . _id ) ;
97
+ await user . save ( ) ;
98
+
99
+ res
100
+ . status ( 200 )
101
+ . json ( { msg : "URL Compressed Successfully" , url : newUrl } ) ;
102
+ }
103
+ } catch ( err ) {
104
+ console . log ( err ) ;
105
+ res . status ( 500 ) . send ( { msg : "Internal Server Error" } ) ;
106
+ }
107
+ } else {
108
+ res . status ( 400 ) . send ( { msg : "Invalid Long URL" } ) ;
109
+ }
76
110
} ) ;
77
111
78
112
router . delete ( "/:id" , middleAuth , async ( req , res ) => {
79
- const id = req . params . id ;
80
-
81
- try {
82
- const url = await Url . findById ( id ) ;
83
-
84
- if ( req . user . id !== url . creator . toString ( ) ) {
85
- return res . status ( 401 ) . json ( { msg : "Unauthorized User" } ) ;
86
- }
87
-
88
- const user = await User . findById ( req . user . id ) ;
89
-
90
- for ( let i = 0 ; i < user . shortenedUrl . length ; i ++ ) {
91
- if ( user . shortenedUrl [ i ] . toString ( ) === id ) {
92
- user . shortenedUrl . splice ( i , 1 ) ;
93
- break ;
94
- }
95
- }
96
- await user . save ( ) ;
97
- await url . deleteOne ( ) ;
98
-
99
- res . status ( 200 ) . json ( { msg : "Short Url Deleted Successfully" } ) ;
100
- } catch ( err ) {
101
- console . log ( err ) ;
102
- res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
103
- }
113
+ const id = req . params . id ;
114
+
115
+ try {
116
+ const url = await Url . findById ( id ) ;
117
+
118
+ if ( req . user . id !== url . creator . toString ( ) ) {
119
+ return res . status ( 401 ) . json ( { msg : "Unauthorized User" } ) ;
120
+ }
121
+
122
+ const user = await User . findById ( req . user . id ) ;
123
+
124
+ for ( let i = 0 ; i < user . shortenedUrl . length ; i ++ ) {
125
+ if ( user . shortenedUrl [ i ] . toString ( ) === id ) {
126
+ user . shortenedUrl . splice ( i , 1 ) ;
127
+ break ;
128
+ }
129
+ }
130
+ await user . save ( ) ;
131
+ await url . deleteOne ( ) ;
132
+
133
+ res . status ( 200 ) . json ( { msg : "Short Url Deleted Successfully" } ) ;
134
+ } catch ( err ) {
135
+ console . log ( err ) ;
136
+ res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
137
+ }
104
138
} ) ;
105
139
106
140
router . patch ( "/:id" , middleAuth , async ( req , res ) => {
107
- const id = req . params . id ;
141
+ const id = req . params . id ;
108
142
109
- try {
110
- const url = await Url . findById ( id ) ;
143
+ try {
144
+ const url = await Url . findById ( id ) ;
111
145
112
- if ( req . user . id !== url . creator . toString ( ) ) {
113
- return res . status ( 401 ) . json ( { msg : "Unauthorized User" } ) ;
114
- }
146
+ if ( req . user . id !== url . creator . toString ( ) ) {
147
+ return res . status ( 401 ) . json ( { msg : "Unauthorized User" } ) ;
148
+ }
115
149
116
- const { lastDate, status } = req . body ;
150
+ const { lastDate, status } = req . body ;
117
151
118
- url . lastDate = lastDate ;
119
- url . status = status ;
120
- const newUrl = await url . save ( ) ;
121
- res . status ( 200 ) . json ( { msg : "URL updated successfully" , url : newUrl } ) ;
122
- } catch ( err ) {
123
- console . log ( err ) ;
124
- res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
125
- }
152
+ url . lastDate = lastDate ;
153
+ url . status = status ;
154
+ const newUrl = await url . save ( ) ;
155
+ res . status ( 200 ) . json ( { msg : "URL updated successfully" , url : newUrl } ) ;
156
+ } catch ( err ) {
157
+ console . log ( err ) ;
158
+ res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
159
+ }
126
160
} ) ;
127
161
128
162
module . exports = router ;
0 commit comments