Skip to content

Commit 5f19143

Browse files
committed
Custom Code function added to the list
2 parents 1590c03 + d7a64a5 commit 5f19143

File tree

5 files changed

+149
-107
lines changed

5 files changed

+149
-107
lines changed

.env-example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
MONGODB_URL = Your mongodb connection URL
2+
3+
JWT_SECRET = Any string to be secret
4+
BASE_URL = App base url

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ An App to manage `minified URLs` and `generate QR Codes` of the minified URLs.
1818

1919
<br />
2020

21-
[Click for Client Repo](https://github.com/HyperLoo/UrlCompressor-Client)
21+
[Click for Server Repo](https://github.com/HyperLoo/UrlCompressor-Server)
2222

2323
## **Login Section 🔑**
2424

@@ -36,6 +36,8 @@ Here one can create a new `Compressed URL` with it's `Expiration Date` if needed
3636

3737
Also, one can see all the `compressed URLs` by him with the `Original URLs` and can copy it for sharing.
3838

39+
One can also choose `Custom Code` for the compressed URL.
40+
3941
<br/><br/>
4042

4143
## **URL Details and Update Section 📝**

app.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ const auth = require("./routes/api/auth");
1111
const Url = require("./models/url");
1212

1313
const app = express();
14+
app.use(cors());
1415

1516
app.use(helmet.permittedCrossDomainPolicies());
1617
app.use(helmet.xssFilter());
1718

1819
app.use(helmet.dnsPrefetchControl({ allow: true }));
19-
app.use(helmet.frameguard({ action: 'sameorigin' }));
20-
app.use(helmet.hidePoweredBy({ setTo: 'your dummy tech' }));
20+
app.use(helmet.frameguard({ action: "sameorigin" }));
21+
app.use(helmet.hidePoweredBy({ setTo: "your dummy tech" }));
2122
app.use(helmet.hsts({ maxAge: 2592000 })); //30 days max age
2223

2324
app.use(bodyParser.urlencoded({ extended: false }));
2425
app.use(bodyParser.json());
25-
app.use(cors());
26+
27+
require("dotenv").config();
2628

2729
mongoose
2830
.connect(
@@ -69,7 +71,7 @@ app.get("/:code", async (req, res) => {
6971
}
7072
if (!url.status) {
7173
return res
72-
.status(200)
74+
.status(404)
7375
.json({ msg: "This url is temporarily out of service!" });
7476
}
7577

routes/api/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const auth = require("../../middleware/auth");
77
const User = require("../../models/user");
88

99
router.post("/", async (req, res) => {
10-
const { name, email, password } = req.body;
10+
const { email, password } = req.body;
1111

1212
if (!email || !password) {
1313
return res.status(400).send({ msg: "Please enter all fields" });

routes/api/url.js

Lines changed: 135 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -9,120 +9,154 @@ const Url = require("../../models/url");
99
const User = require("../../models/user");
1010

1111
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" });
2751
});
2852

2953
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+
}
76110
});
77111

78112
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+
}
104138
});
105139

106140
router.patch("/:id", middleAuth, async (req, res) => {
107-
const id = req.params.id;
141+
const id = req.params.id;
108142

109-
try {
110-
const url = await Url.findById(id);
143+
try {
144+
const url = await Url.findById(id);
111145

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+
}
115149

116-
const { lastDate, status } = req.body;
150+
const { lastDate, status } = req.body;
117151

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+
}
126160
});
127161

128162
module.exports = router;

0 commit comments

Comments
 (0)