forked from Chia-Network/cadt
-
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.
Merge pull request #146 from Chia-Network/feat/address-book-backend
feat: add address book module
- Loading branch information
Showing
16 changed files
with
363 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import _ from 'lodash'; | ||
import { AddressBook } from '../models'; | ||
import { | ||
paginationParams, | ||
optionallyPaginatedResponse, | ||
} from '../utils/helpers'; | ||
import { genericSortColumnRegex } from '../utils/string-utils'; | ||
|
||
export const findAll = async (req, res) => { | ||
try { | ||
let { page, limit, order } = req.query; | ||
|
||
let pagination = paginationParams(page, limit); | ||
|
||
// default to DESC | ||
let resultOrder = [['createdAt', 'DESC']]; | ||
|
||
if (order?.match(genericSortColumnRegex)) { | ||
const matches = order.match(genericSortColumnRegex); | ||
resultOrder = [[matches[1], matches[2]]]; | ||
} | ||
|
||
let results; | ||
|
||
results = await AddressBook.findAndCountAll({ | ||
order: resultOrder, | ||
...pagination, | ||
}); | ||
|
||
return res.json(optionallyPaginatedResponse(results, page, limit)); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Can not retrieve address book data', | ||
error: error.message, | ||
success: false, | ||
}); | ||
} | ||
}; | ||
|
||
export const findOne = async (req, res) => { | ||
try { | ||
const query = { | ||
where: { id: req.query.id }, | ||
}; | ||
|
||
res.json(await AddressBook.findOne(query)); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Error retrieving address', | ||
error: error.message, | ||
success: false, | ||
}); | ||
} | ||
}; | ||
|
||
export const create = async (req, res) => { | ||
try { | ||
const newRecord = _.cloneDeep(req.body); | ||
|
||
await AddressBook.create({ | ||
name: newRecord.name, | ||
walletAddress: newRecord.walletAddress, | ||
}); | ||
|
||
res.json({ | ||
message: 'successfully created address book entry', | ||
success: true, | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: error.message, | ||
success: false, | ||
}); | ||
} | ||
}; | ||
|
||
export const update = async (req, res) => { | ||
try { | ||
const data = _.cloneDeep(req.body); | ||
const id = data.id; | ||
|
||
await AddressBook.update( | ||
{ | ||
...data, | ||
}, | ||
{ | ||
where: { | ||
id, | ||
}, | ||
}, | ||
); | ||
|
||
res.json({ | ||
message: 'successfully updated address book entry', | ||
success: true, | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: error.message, | ||
success: false, | ||
}); | ||
} | ||
}; | ||
|
||
export const destroy = async (req, res) => { | ||
try { | ||
const data = _.cloneDeep(req.body); | ||
const id = data.id; | ||
|
||
const result = await AddressBook.destroy({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
if (result) | ||
res.json({ | ||
message: 'Address deleted successfully', | ||
success: true, | ||
}); | ||
else res.status(204).json({ message: 'No rows deleted', success: false }); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: error.message, | ||
success: 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
34 changes: 34 additions & 0 deletions
34
src/database/migrations/20241127214244-create-address-book.js
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,34 @@ | ||
'use strict'; | ||
/** @type {import('sequelize-cli').Migration} */ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
export default { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('addressBook', { | ||
id: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true, | ||
defaultValue: uuidv4(), | ||
primaryKey: true, | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
}, | ||
walletAddress: { | ||
type: Sequelize.STRING, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
}); | ||
}, | ||
async down(queryInterface) { | ||
await queryInterface.dropTable('addressBook'); | ||
}, | ||
}; |
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 stub from './address-book.stub.js'; | ||
|
||
export const StatisticsMock = { | ||
findAll: () => stub, | ||
}; |
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,53 @@ | ||
'use strict'; | ||
|
||
import ModelTypes from './address-book.modeltypes.cjs'; | ||
import Sequelize from 'sequelize'; | ||
const { Model } = Sequelize; | ||
import { sequelize, safeMirrorDbHandler } from '../../database/index.js'; | ||
import { AddressBookMirror } from './address-book.model.mirror.js'; | ||
|
||
class AddressBook extends Model { | ||
static async create(values, options) { | ||
safeMirrorDbHandler(async () => { | ||
const mirrorOptions = { | ||
...options, | ||
transaction: options?.mirrorTransaction, | ||
}; | ||
await AddressBookMirror.create(values, mirrorOptions); | ||
}); | ||
return super.create(values, options); | ||
} | ||
|
||
static async update(values, options) { | ||
safeMirrorDbHandler(async () => { | ||
const mirrorOptions = { | ||
...options, | ||
transaction: options?.mirrorTransaction, | ||
}; | ||
await AddressBookMirror.update(values, mirrorOptions); | ||
}); | ||
return super.update(values, options); | ||
} | ||
|
||
static async destroy(options) { | ||
safeMirrorDbHandler(async () => { | ||
const mirrorOptions = { | ||
...options, | ||
transaction: options?.mirrorTransaction, | ||
}; | ||
await AddressBookMirror.destroy(mirrorOptions); | ||
}); | ||
return super.destroy(options); | ||
} | ||
} | ||
|
||
AddressBook.init(ModelTypes, { | ||
sequelize, | ||
modelName: 'addressBook', | ||
freezeTableName: true, | ||
timestamps: true, | ||
createdAt: true, | ||
updatedAt: true, | ||
}); | ||
|
||
export { AddressBook }; |
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,29 @@ | ||
'use strict'; | ||
|
||
import Sequelize from 'sequelize'; | ||
const { Model } = Sequelize; | ||
|
||
import { sequelizeMirror, safeMirrorDbHandler } from '../../database'; | ||
import ModelTypes from './address-book.modeltypes.cjs'; | ||
|
||
class AddressBookMirror extends Model {} | ||
|
||
safeMirrorDbHandler(() => { | ||
AddressBookMirror.init(ModelTypes, { | ||
sequelize: sequelizeMirror, | ||
modelName: 'addressBook', | ||
freezeTableName: true, | ||
timezone: '+00:00', | ||
define: { | ||
charset: 'utf8mb4', | ||
collate: 'utf8mb4_general_ci', | ||
}, | ||
dialectOptions: { | ||
charset: 'utf8mb4', | ||
dateStrings: true, | ||
typeCast: true, | ||
}, | ||
}); | ||
}); | ||
|
||
export { AddressBookMirror }; |
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,26 @@ | ||
const Sequelize = require('sequelize'); | ||
const { uuid: uuidv4 } = require('uuidv4'); | ||
|
||
module.exports = { | ||
id: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true, | ||
defaultValue: () => uuidv4(), | ||
primaryKey: true, | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
}, | ||
walletAddress: { | ||
type: Sequelize.STRING, | ||
}, | ||
createdAt: { | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.NOW, | ||
}, | ||
updatedAt: { | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.NOW, | ||
}, | ||
}; |
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 @@ | ||
export default []; |
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,2 @@ | ||
export * from './address-book.model'; | ||
export * from './address-book.mock'; |
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,45 @@ | ||
'use strict'; | ||
|
||
import express from 'express'; | ||
import { AddressBookController } from '../../../controllers'; | ||
import joiExpress from 'express-joi-validation'; | ||
|
||
import { | ||
addressBookPostSchema, | ||
addressBookGetQuerySchema, | ||
addressBookUpdateSchema, | ||
addressBookDeleteSchema, | ||
} from '../../../validations'; | ||
|
||
const validator = joiExpress.createValidator({ passError: true }); | ||
const AddressBookRouter = express.Router(); | ||
|
||
AddressBookRouter.get( | ||
'/', | ||
validator.query(addressBookGetQuerySchema), | ||
(req, res) => { | ||
return req.query.id | ||
? AddressBookController.findOne(req, res) | ||
: AddressBookController.findAll(req, res); | ||
}, | ||
); | ||
|
||
AddressBookRouter.post( | ||
'/', | ||
validator.body(addressBookPostSchema), | ||
AddressBookController.create, | ||
); | ||
|
||
AddressBookRouter.put( | ||
'/', | ||
validator.body(addressBookUpdateSchema), | ||
(req, res) => AddressBookController.update(req, res, false), | ||
); | ||
|
||
AddressBookRouter.delete( | ||
'/', | ||
validator.body(addressBookDeleteSchema), | ||
AddressBookController.destroy, | ||
); | ||
|
||
export { AddressBookRouter }; |
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
Oops, something went wrong.