Skip to content

Commit

Permalink
added endpoints of subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
shebz2023 committed Mar 16, 2024
1 parent 4ffdaf3 commit 97aaea9
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 0 deletions.
114 changes: 114 additions & 0 deletions api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,120 @@ paths:
status: "success"
data: []

/subs:
post:
tags:
- "SUBS"
summary: "subscribe"
consumes:
- application/json
parameters:
- in: body
name: "email"
description: "Message object"
required: true
schema:
type: "object"
properties:
email:
type: "string"
responses:
'200':
description: "OK"
schema:
type: "object"
properties:
status:
type: "string"
data:
type: "array"
items: {}
examples:
success:
status: "success"
data: []

get:
tags:
- "SUBS"
summary: "Get all subs"
security:
- Token: []
responses:
'200':
description: "OK"
schema:
type: "object"
properties:
status:
type: "string"
data:
type: "array"
items: {}
examples:
success:
status: "success"
data: []

/subs/{id}:
get:
tags:
- "SUBS"
summary: "Get Subs by id"
security:
- Token: []
description: "Get subs by id"
parameters:
- name: "id"
in: "path"
description: "ID of the subscriber"
required: true
type: "string"
responses:
'200':
description: "OK"
schema:
type: "object"
properties:
status:
type: "string"
data:
type: "array"
items: {}
examples:
success:
status: "success"
data: []

delete:
tags:
- "SUBS"
summary: "Delete a subscriber"
description: "Delete a subscriber"
security:
- Token: []
parameters:
- name: "id"
in: "path"
description: "ID of the subs"
required: true
type: "string"
responses:
'200':
description: "OK"
schema:
type: "object"
properties:
status:
type: "string"
data:
type: "array"
items: {}
examples:
success:
status: "success"
data: []

/users/:
get:
tags:
Expand Down
85 changes: 85 additions & 0 deletions dist/controllers/subs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteSubs = exports.createSubs = exports.getSubs = exports.getAllSubs = void 0;
const Sub_1 = __importDefault(require("../models/Sub"));
const subs_1 = require("../validations/subs");
// Get all subscribers
const getAllSubs = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const subscribers = yield Sub_1.default.find();
res.status(200).json(subscribers);
}
catch (error) {
console.log(error);
res.status(500).json({ error: 'Failed to retrieve subscribers' });
}
});
exports.getAllSubs = getAllSubs;
// Get one subscriber by ID
const getSubs = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const subscriberId = req.params.id;
const subscriber = yield Sub_1.default.findById(subscriberId);
if (subscriber) {
res.status(200).json(subscriber);
}
else {
res.status(404).json({ error: 'Subscriber not found' });
}
}
catch (error) {
console.log(error);
res.status(500).json({ error: 'Failed to retrieve subscriber' });
}
});
exports.getSubs = getSubs;
// Create a new subscriber
const createSubs = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { email } = req.body;
const { error } = subs_1.subVal.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}
const newSubscriber = new Sub_1.default({
email,
});
const savedSubscriber = yield newSubscriber.save();
res.status(201).json({ subscriber: savedSubscriber, message: 'Subscriber added successfully' });
}
catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
}
});
exports.createSubs = createSubs;
// Delete a subscriber by ID
const deleteSubs = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const subscriberId = req.params.id;
const deletedSubscriber = yield Sub_1.default.findByIdAndDelete(subscriberId);
if (deletedSubscriber) {
res.status(200).json({ message: 'Subscriber deleted successfully' });
}
else {
res.status(404).json({ error: 'Subscriber not found' });
}
}
catch (error) {
console.log(error);
res.status(500).json({ error: 'Failed to delete subscriber' });
}
});
exports.deleteSubs = deleteSubs;
15 changes: 15 additions & 0 deletions dist/models/Sub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongoose_1 = __importDefault(require("mongoose"));
const Schema = mongoose_1.default.Schema;
const subscriberSchema = new Schema({
email: {
type: String,
required: true,
unique: true,
},
});
exports.default = mongoose_1.default.model("Subscriber", subscriberSchema);
6 changes: 6 additions & 0 deletions dist/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const liking_1 = require("../controllers/liking");
const messages_1 = require("../controllers/messages");
const users_1 = require("../controllers/users");
const auth_1 = require("../middlewares/auth");
const subs_1 = require("../controllers/subs");
const router = express_1.default.Router();
// blogs
router.get('/blogs', blogs_1.getAllBlogs);
Expand All @@ -24,6 +25,11 @@ router.get('/messages', auth_1.authenticateUser, auth_1.authorizeAdmin, messages
router.get('/messages/:id', auth_1.authenticateUser, auth_1.authorizeAdmin, messages_1.getMessageById);
router.post('/messages', messages_1.createMessage);
router.delete('/messages/:id', auth_1.authenticateUser, auth_1.authorizeAdmin, messages_1.deleteMessageById);
// subs
router.get('/subs', auth_1.authenticateUser, auth_1.authorizeAdmin, subs_1.getAllSubs);
router.get('/subs/:id', auth_1.authenticateUser, auth_1.authorizeAdmin, subs_1.getSubs);
router.post('/subs', auth_1.authenticateUser, auth_1.authorizeAdmin, subs_1.createSubs);
router.delete('/subs/:id', auth_1.authenticateUser, auth_1.authorizeAdmin, subs_1.deleteSubs);
// accounts
router.get('/users', auth_1.authenticateUser, auth_1.authorizeAdmin, users_1.getAllUsers);
router.post('/login', users_1.login);
Expand Down
10 changes: 10 additions & 0 deletions dist/validations/subs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.subVal = void 0;
const joi_1 = __importDefault(require("joi"));
exports.subVal = joi_1.default.object({
email: joi_1.default.string().email().required(),
}).unknown(false);
68 changes: 68 additions & 0 deletions src/controllers/subs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Request, Response } from 'express';
import Subscriber from '../models/Sub';
import { subVal } from '../validations/subs';

// Get all subscribers
export const getAllSubs = async (req: Request, res: Response) => {
try {
const subscribers = await Subscriber.find();
res.status(200).json(subscribers);
} catch (error) {
console.log(error);
res.status(500).json({ error: 'Failed to retrieve subscribers' });
}
};

// Get one subscriber by ID
export const getSubs = async (req: Request, res: Response) => {
try {
const subscriberId = req.params.id;
const subscriber = await Subscriber.findById(subscriberId);

if (subscriber) {
res.status(200).json(subscriber);
} else {
res.status(404).json({ error: 'Subscriber not found' });
}
} catch (error) {
console.log(error);
res.status(500).json({ error: 'Failed to retrieve subscriber' });
}
};

// Create a new subscriber
export const createSubs = async (req: Request, res: Response) => {
try {
const { email } = req.body;
const { error } = subVal.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}

const newSubscriber = new Subscriber({
email,
});
const savedSubscriber = await newSubscriber.save();
res.status(201).json({ subscriber: savedSubscriber, message: 'Subscriber added successfully' });
} catch (error) {
console.log(error);
res.status(400).json({ message: (error as Error).message });
}
};

// Delete a subscriber by ID
export const deleteSubs = async (req: Request, res: Response) => {
try {
const subscriberId = req.params.id;
const deletedSubscriber = await Subscriber.findByIdAndDelete(subscriberId);

if (deletedSubscriber) {
res.status(200).json({ message: 'Subscriber deleted successfully' });
} else {
res.status(404).json({ error: 'Subscriber not found' });
}
} catch (error) {
console.log(error)
res.status(500).json({ error: 'Failed to delete subscriber' });
}
};
13 changes: 13 additions & 0 deletions src/models/Sub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import mongoose from "mongoose";

const Schema = mongoose.Schema;

const subscriberSchema = new Schema({
email: {
type: String,
required: true,
unique: true,
},
});

export default mongoose.model("Subscriber", subscriberSchema);
7 changes: 7 additions & 0 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { likePost } from '../controllers/liking';
import { createMessage, deleteMessageById, getAllMessages, getMessageById } from '../controllers/messages';
import { createUser, deleteUserByEmail, getAllUsers, login } from '../controllers/users';
import { authenticateUser, authorizeAdmin } from '../middlewares/auth';
import { getSubs,getAllSubs,createSubs,deleteSubs } from '../controllers/subs';

const router = express.Router();

Expand All @@ -25,6 +26,12 @@ router.get('/messages/:id', authenticateUser,authorizeAdmin,getMessageById);
router.post('/messages', createMessage);
router.delete('/messages/:id', authenticateUser,authorizeAdmin,deleteMessageById);

// subs
router.get('/subs',authenticateUser,authorizeAdmin,getAllSubs);
router.get('/subs/:id',authenticateUser,authorizeAdmin,getSubs);
router.post('/subs',authenticateUser,authorizeAdmin,createSubs);
router.delete('/subs/:id',authenticateUser,authorizeAdmin,deleteSubs)


// accounts
router.get('/users',authenticateUser,authorizeAdmin, getAllUsers);
Expand Down
5 changes: 5 additions & 0 deletions src/validations/subs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Joi from "joi";

export const subVal = Joi.object({
email: Joi.string().email().required(),
}).unknown(false);

0 comments on commit 97aaea9

Please sign in to comment.