-
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
791a943
commit 3a1b620
Showing
11 changed files
with
256 additions
and
2 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
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,57 @@ | ||
/* | ||
* =================== MTS TECHNONATURA SERVER ================= | ||
* | ||
* This API Script under MIT LICENSE | ||
* What is this for ? This is REST API for MTS Technonatura arduino database, user make app and saved the app to db. | ||
* | ||
* (c) 2021 by MTS-Technonatura, made with 💖 by Aldhan | ||
* ============================================================= | ||
*/ | ||
|
||
interface Errors { | ||
desc: string; | ||
name: string; | ||
} | ||
|
||
// handle errors | ||
async function handleErrors(err: { | ||
message: string; | ||
code: number; | ||
_message: string; | ||
keyValue: { | ||
name?: string; | ||
title?: string; | ||
}; | ||
}) { | ||
// @ts-ignore | ||
let errors: Errors = {}; | ||
|
||
if (err.message == 'Only Letters and Numbers are allowed') { | ||
errors.name = err.message; | ||
} | ||
|
||
// duplicate username error | ||
if (err.code === 11000 && err.keyValue.name) { | ||
errors.name = 'that name is already registered'; | ||
} | ||
|
||
// validation errors | ||
if ( | ||
err._message && | ||
err._message.includes('TechnoNaturaBranch validation failed') | ||
) { | ||
// @ts-ignore | ||
Object.values(err.errors).forEach(({ properties }) => { | ||
// console.log(val); | ||
// console.log(properties); | ||
if (properties.message) { | ||
// @ts-ignore | ||
errors[properties.path] = properties.message; | ||
} | ||
}); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
export default handleErrors; |
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,32 @@ | ||
/* | ||
* =================== TECHNONATURA SERVER ================= | ||
* | ||
* This API Script under MIT LICENSE | ||
* | ||
* (c) 2021 by Aldhan | ||
* ============================================================= | ||
*/ | ||
|
||
import * as express from 'express'; | ||
|
||
import TechnoNaturaBranch from '../../models/TechnoNatura-Branch/TechnoNatura-Branch.model'; | ||
|
||
const TechnoNaturaBranchRouter = express.Router(); | ||
|
||
TechnoNaturaBranchRouter.get('/branches', async (req, res) => { | ||
try { | ||
const branches = await TechnoNaturaBranch.find(); | ||
res.json({ | ||
message: 'Success fetching branches!', | ||
status: 'success', | ||
branches, | ||
}); | ||
} catch (err) { | ||
res.json({ | ||
message: 'Error when fetching TechnoNatura Branches', | ||
status: 'error', | ||
}); | ||
} | ||
}); | ||
|
||
export default TechnoNaturaBranchRouter; |
65 changes: 65 additions & 0 deletions
65
src/models/TechnoNatura-Branch/TechnoNatura-Branch.model.ts
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,65 @@ | ||
import { Schema, Model, Document, model, Types, Query, Error } from 'mongoose'; | ||
|
||
import { TechnonaturaBranchI } from './index'; | ||
|
||
export { TechnonaturaBranchI }; | ||
|
||
export interface TechnonaturaBranchBaseDocument | ||
extends TechnonaturaBranchI, | ||
Document {} | ||
|
||
// Export this for strong typing | ||
export interface TechnonaturaBranchDocument | ||
extends TechnonaturaBranchBaseDocument {} | ||
|
||
// For model | ||
export interface TechnonaturaBranchModel | ||
extends Model<TechnonaturaBranchBaseDocument> {} | ||
|
||
const userSchema = new Schema< | ||
TechnonaturaBranchDocument, | ||
TechnonaturaBranchModel | ||
>({ | ||
title: { | ||
type: String, | ||
required: [true, 'Please enter your name'], | ||
minlength: [4, 'Minimum name length is 4 characters'], | ||
}, | ||
active: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
name: { | ||
type: String, | ||
lowercase: true, | ||
unique: true, | ||
required: [true, 'username cannot be blank'], | ||
validate: [ | ||
validateTechnonaturaBranchname, | ||
'Only Letters and Numbers are allowed', | ||
], | ||
}, | ||
}); | ||
|
||
function validateTechnonaturaBranchname(str: string) { | ||
if (!str.match(/^[A-Za-z0-9_-]*$/)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// fire a function before doc saved to db | ||
// userSchema.pre('save', async function(next) { | ||
// // console.log('hello'); | ||
// const salt = await bcrypt.genSalt(); | ||
// this.password = await bcrypt.hash(this.password, salt); | ||
// // this.email = await EncryptEmail(this.email); | ||
|
||
// next(); | ||
// }); | ||
|
||
export default model<TechnonaturaBranchDocument, TechnonaturaBranchModel>( | ||
'TechnoNaturaBranch', | ||
userSchema, | ||
); |
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 @@ | ||
export interface TechnonaturaBranchI { | ||
title: string; | ||
name: string; | ||
active: boolean; | ||
} |
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
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,41 @@ | ||
/* | ||
* =================== TECHNONATURA SERVER ================= | ||
* | ||
* This API Script under MIT LICENSE | ||
* What is this for ? This is REST API for MTS Technonatura arduino database, user make app and saved the app to db. | ||
* | ||
* (c) 2021 by Aldhan | ||
* ============================================================= | ||
*/ | ||
|
||
import * as express from 'express'; | ||
|
||
import TechnoNaturaBranch from '../models/TechnoNatura-Branch/TechnoNatura-Branch.model'; | ||
import TechnoNaturaBranchHandleErrors from '../controllers/branch/handleErrors'; | ||
|
||
const TechnoNaturaBranchRouter = express.Router(); | ||
|
||
TechnoNaturaBranchRouter.post('/add', async (req, res) => { | ||
const { title, name } = req.body; | ||
|
||
try { | ||
const branch = new TechnoNaturaBranch({ title, name }); | ||
await branch.save(); | ||
res.json({ | ||
message: 'Success add branch!', | ||
status: 'success', | ||
branch, | ||
}); | ||
return; | ||
} catch (err) { | ||
const errors = await TechnoNaturaBranchHandleErrors(err); | ||
res.json({ | ||
message: 'Error when fetching TechnoNatura Branches', | ||
status: 'error', | ||
errors: errors, | ||
}); | ||
return; | ||
} | ||
}); | ||
|
||
export default TechnoNaturaBranchRouter; |
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