Skip to content

Instantly share code, notes, and snippets.

View dipeshhkc's full-sized avatar
🏠
Working from home

Dipesh KC dipeshhkc

🏠
Working from home
View GitHub Profile
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
# Request definition
[request_definition]
r = sub, obj, act
# Policy definition
[policy_definition]
p = sub, obj, act
# Policy effect
[policy_effect]
@dipeshhkc
dipeshhkc / formik-error-focus.js
Last active June 10, 2021 09:19
Scroll To the First Error Element [FORMIK]
//(1/3) Snippet to getting first error(works even if nested case)
import { isObject } from "lodash";
export const getFirstErrorKey = (object: any, keys: string[] = []): any => {
let firstErrorKey = "";
if (Array.isArray(object)) {
for (let i = 0; i < object.length; i++) {
if (object[i]) {
firstErrorKey = Object.keys(object)[i];
break;
// WithTrx enables repository with transaction
func (u userRepository) WithTrx(trxHandle *gorm.DB) userRepository {
if trxHandle == nil {
log.Print("Transaction Database not found")
return u
}
u.DB = trxHandle
return u
}
func (u userRepository) IncrementMoney(receiver uint, amount float64) error {
// WithTrx enables service with repository containing of transaction
func (u userService) WithTrx(trxHandle *gorm.DB) userService {
u.userRepository = u.userRepository.WithTrx(trxHandle)
return u
}
func (u userService) IncrementMoney(receiver uint, amount float64) error {
return u.userRepository.IncrementMoney(receiver, amount)
}
func (u userController) TransferMoney(c *gin.Context) {
log.Print("[UserController]...get all Users")
txHandle := c.MustGet("db_trx").(*gorm.DB)
var moneyTransfer model.MoneyTransfer
if err := c.ShouldBindJSON(&moneyTransfer); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
router.POST("/money-transfer", middleware.DBTransactionMiddleware(db), userController.TransferMoney)
//StatusInList -> checks if the given status is in the list
func StatusInList(status int, statusList []int) bool {
for _, i := range statusList {
if i == status {
return true
}
}
return false
}
// begin a transaction
tx := db.Begin()
// do some database operations in the transaction (use 'tx' from this point, not 'db')
tx.Create(...)
// ...
// rollback the transaction in case of error
tx.Rollback()
=CONCATENATE("INSERT INTO gifts(name,gift_image,coin_value,category,created_at,updated_at) values(","'",A1,"','gif/",B1,".gif',",J1,",'",K1,"','2021-02-23 10:37:32','2021-02-23 10:37:32'",");")