-
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
1 parent
3c36a8d
commit dc824bb
Showing
7 changed files
with
265 additions
and
167 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
const subscribersService = require("../service/subscribers.service"); | ||
const catchAsync = require('../utils/catchAsync'); | ||
const { Response } = require("../utils/response"); | ||
const catchAsync = require("../utils/catchAsync"); | ||
const { Response } = require("../utils/response"); | ||
|
||
const httpStatus = require('http-status'); | ||
const httpStatus = require("http-status"); | ||
|
||
const insertSubscriber = catchAsync(async (req, res) => { | ||
const DTO = await subscribersService.insertSubscriber(req.body); | ||
Response(res, httpStatus.CREATED, DTO.message, DTO.data); | ||
const DTO = await subscribersService.insertSubscriber(req.body); | ||
Response(res, httpStatus.CREATED, DTO.message, DTO.data); | ||
}); | ||
|
||
const getAllSubscribers = catchAsync(async (req, res) => { | ||
const DTO = await subscribersService.getAllSubscribers(); | ||
Response(res, httpStatus.OK, DTO.message, DTO.data); | ||
}); | ||
|
||
const deleteSubscriber = catchAsync(async (req, res) => { | ||
const DTO = await subscribersService.deleteSubscriber(req.params); | ||
Response(res, httpStatus.OK, DTO.message, DTO.data); | ||
}); | ||
|
||
exports.subscribersController = { | ||
insertSubscriber, | ||
getAllSubscribers, | ||
deleteSubscriber, | ||
}; |
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 |
---|---|---|
@@ -1,9 +1,6 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const { celebrate, Joi, Segments } = require("celebrate"); | ||
const httpStatus = require("http-status"); | ||
|
||
const {DefaultResponse} = require("../../utils/DefaultResponse"); | ||
|
||
const { | ||
subscribersController, | ||
|
@@ -12,8 +9,8 @@ const { | |
var insertSubscriberValidationSchema = { | ||
[Segments.BODY]: Joi.object().keys({ | ||
email: Joi.string().required().email().messages({ | ||
'email': 'Email is required and follow pattern [email protected]', | ||
}) | ||
email: "Email is required and follow pattern [email protected]", | ||
}), | ||
}), | ||
}; | ||
|
||
|
@@ -23,4 +20,8 @@ router.post( | |
subscribersController.insertSubscriber | ||
); | ||
|
||
router.get("/", subscribersController.getAllSubscribers); | ||
|
||
router.delete("/:id", subscribersController.deleteSubscriber); | ||
|
||
module.exports = router; |
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 |
---|---|---|
@@ -1,35 +1,79 @@ | ||
const subscribersRepository = require("../repository/subscribers.repository"); | ||
const ApiError = require("../utils/ApiError"); | ||
const httpStatus = require('http-status'); | ||
const httpStatus = require("http-status"); | ||
|
||
const insertSubscriber = async ({ email }) => { | ||
try { | ||
const existSubscriber = | ||
await subscribersRepository.findExistedSubscriber(email); | ||
try { | ||
const existSubscriber = await subscribersRepository.findExistedSubscriber( | ||
); | ||
|
||
console.log(existSubscriber); | ||
console.log(existSubscriber); | ||
|
||
if (existSubscriber) { | ||
throw new ApiError(httpStatus.BAD_REQUEST, "Email address subscribed already"); | ||
} | ||
if (existSubscriber) { | ||
throw new ApiError( | ||
httpStatus.BAD_REQUEST, | ||
"Email address subscribed already" | ||
); | ||
} | ||
|
||
const response = await subscribersRepository.insertNewSubscriber(email); | ||
const response = await subscribersRepository.insertNewSubscriber(email); | ||
|
||
return { | ||
message: `Insert subscriber success with id ${response._id}`, | ||
data: response | ||
} | ||
} catch (error) { | ||
let statusCode = httpStatus.INTERNAL_SERVER_ERROR; | ||
return { | ||
message: `Insert subscriber success with id ${response._id}`, | ||
data: response, | ||
}; | ||
} catch (error) { | ||
let statusCode = httpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
if (error.statusCode) { | ||
statusCode = error.statusCode; | ||
} | ||
if (error.statusCode) { | ||
statusCode = error.statusCode; | ||
} | ||
|
||
throw new ApiError(statusCode, error.message); | ||
} | ||
throw new ApiError(statusCode, error.message); | ||
} | ||
}; | ||
|
||
const getAllSubscribers = async () => { | ||
try { | ||
const subscribers = await subscribersRepository.getAllSubscribers(); | ||
|
||
return { | ||
message: "Get subscribers successfull", | ||
data: subscribers, | ||
}; | ||
} catch (error) { | ||
let statusCode = httpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
if (error.statusCode) { | ||
statusCode = error.statusCode; | ||
} | ||
|
||
throw new ApiError(statusCode, error.message); | ||
} | ||
}; | ||
|
||
const deleteSubscriber = async ({ id }) => { | ||
try { | ||
const res = await subscribersRepository.deleteSubscriberById(id); | ||
|
||
return { | ||
message: "Delete successfull", | ||
data: res, | ||
}; | ||
} catch (error) { | ||
let statusCode = httpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
if (error.statusCode) { | ||
statusCode = error.statusCode; | ||
} | ||
|
||
throw new ApiError(statusCode, error.message); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
insertSubscriber, | ||
getAllSubscribers, | ||
deleteSubscriber, | ||
}; |
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 |
---|---|---|
@@ -1,25 +1,41 @@ | ||
import axios from 'axios'; | ||
import axios from "axios"; | ||
|
||
const API_URL = import.meta.env.VITE_API_URL; | ||
|
||
const axiosConfig = { | ||
baseURL: API_URL, | ||
|
||
baseURL: API_URL, | ||
}; | ||
|
||
const instance = axios.create(axiosConfig); | ||
|
||
instance.insertSubscriberHandler = async (email) => { | ||
return (await instance.post('subscribers', | ||
{ | ||
email: email | ||
}, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json' | ||
return await instance.post( | ||
"subscribers", | ||
{ | ||
email: email, | ||
}, | ||
{ | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
})) | ||
} | ||
); | ||
}; | ||
|
||
instance.getSubscribersHandler = async () => { | ||
return await instance.get("subscribers", { | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
}; | ||
|
||
instance.deleteSubscribersByIdHandler = async (id) => { | ||
return await instance.delete(`subscribers/${id}`, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
}; | ||
|
||
export const Axios = instance; | ||
export const Axios = instance; |
Oops, something went wrong.