-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |