step 15 : add master dokter dengan foto dan tanpa foto
This commit is contained in:
@@ -4,11 +4,17 @@ import (
|
||||
"cpone/db"
|
||||
"cpone/models"
|
||||
dbx "cpone/package/database"
|
||||
"cpone/utils"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -851,7 +857,7 @@ func (su *ServicesMdDoctor) GetMdDoctorCode(tipe string) (string, error) {
|
||||
defer logger.Sync()
|
||||
|
||||
query := `
|
||||
select fn_numbering(?) as doctorCode
|
||||
select fn_numbering_cpone(?) as doctorCode
|
||||
`
|
||||
|
||||
logger.Info("QUERY DOCTOR CODE",
|
||||
@@ -1012,6 +1018,12 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
M_DoctorAddressDistrict := ""
|
||||
M_DoctorAddressState := ""
|
||||
M_DoctorAddressCountry := ""
|
||||
M_DoctorAddressVillage := ""
|
||||
M_DoctorAddressRT := ""
|
||||
M_DoctorAddressRW := ""
|
||||
M_DoctorPhoto := ""
|
||||
M_DoctorPhotoLastUpdated := "0000-00-00 00:00:00"
|
||||
M_DoctorPhotoLastUpdatedUserID := 0
|
||||
// VARIABLE END
|
||||
|
||||
// CHECK PARAMS
|
||||
@@ -1099,6 +1111,93 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
M_DoctorAddressDistrict = inp.M_DoctorAddressDistrict
|
||||
M_DoctorAddressState = inp.M_DoctorAddressState
|
||||
M_DoctorAddressCountry = inp.M_DoctorAddressCountry
|
||||
M_DoctorAddressVillage = inp.M_DoctorAddressVillage
|
||||
}
|
||||
|
||||
if inp.M_DoctorAddressRT != "" {
|
||||
M_DoctorAddressRT = inp.M_DoctorAddressRT
|
||||
}
|
||||
|
||||
if inp.M_DoctorAddressRW != "" {
|
||||
M_DoctorAddressRW = inp.M_DoctorAddressRW
|
||||
}
|
||||
|
||||
// foto
|
||||
// inp.M_DoctorPhoto sudah dalam bentuk base64string
|
||||
if inp.M_DoctorPhoto != "" {
|
||||
var ext string
|
||||
M_DoctorPhotoBase64 := inp.M_DoctorPhoto
|
||||
fileName := M_DoctorCode + time.Now().String()
|
||||
|
||||
// Remove base64 prefix if present
|
||||
// if strings.HasPrefix(M_DoctorPhotoBase64, "data:image/") {
|
||||
// M_DoctorPhotoBase64 = M_DoctorPhotoBase64[strings.IndexByte(M_DoctorPhotoBase64, ',')+1:]
|
||||
// }
|
||||
|
||||
// Check if the base64 string has a valid prefix
|
||||
if strings.HasPrefix(M_DoctorPhotoBase64, "data:image/") {
|
||||
// Extract the file type from the prefix
|
||||
fileType := strings.Split(M_DoctorPhotoBase64[11:], ";")[0]
|
||||
switch fileType {
|
||||
case "png":
|
||||
ext = ".png"
|
||||
case "jpeg":
|
||||
ext = ".jpeg"
|
||||
case "jpg":
|
||||
ext = ".jpg"
|
||||
// case "gif":
|
||||
// ext = ".gif"
|
||||
default:
|
||||
return data, fmt.Errorf("unsupported file type")
|
||||
}
|
||||
// Remove the prefix from the base64 string
|
||||
M_DoctorPhotoBase64 = M_DoctorPhotoBase64[strings.IndexByte(M_DoctorPhotoBase64, ',')+1:]
|
||||
} else {
|
||||
return data, fmt.Errorf("invalid base64 string")
|
||||
}
|
||||
|
||||
// Decode base64 string to byte array
|
||||
decodedBytes, err := base64.StdEncoding.DecodeString(M_DoctorPhotoBase64)
|
||||
if err != nil {
|
||||
// return data, fmt.Errorf("error decoding base64 string: %v", err)
|
||||
defer logger.Sync()
|
||||
logger.Error("Error Decoding Base64 String",
|
||||
zap.Any("error decoding base64 string : %v", err),
|
||||
)
|
||||
return data, fmt.Errorf("error decoding base64 string: %v", err)
|
||||
}
|
||||
|
||||
_, b, _, _ := runtime.Caller(0)
|
||||
basepath := filepath.Dir(b)
|
||||
println("Basepath")
|
||||
println(basepath)
|
||||
|
||||
// ext := filepath.Ext(fileName)
|
||||
newFileName := utils.GenerateRandomID(fileName) + ext
|
||||
uploadDir := filepath.Join(basepath, "..", "..", "uploads", "doctor")
|
||||
println(uploadDir)
|
||||
|
||||
// Menentukan path file tujuan
|
||||
dstPath := filepath.Join(uploadDir, newFileName)
|
||||
// Membuat direktori jika belum ada
|
||||
if err := os.MkdirAll(uploadDir, os.ModePerm); err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Error("Error Mkdir",
|
||||
zap.Any("error mdir : %v", err),
|
||||
)
|
||||
return data, fmt.Errorf("error mkdir: %v", err)
|
||||
}
|
||||
|
||||
// Menulis konten decoded base64 ke file tujuan
|
||||
if err := os.WriteFile(dstPath, decodedBytes, 0644); err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Error("Error Writing File",
|
||||
zap.Any("error writing file : %v", err),
|
||||
)
|
||||
return data, fmt.Errorf("error writing file: %v", err)
|
||||
}
|
||||
|
||||
M_DoctorPhoto = newFileName
|
||||
}
|
||||
|
||||
// CHECK PARAMS
|
||||
@@ -1142,7 +1241,13 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
M_DoctorAddressCountry,
|
||||
M_DoctorIsActive,
|
||||
M_DoctorCreatedUserID,
|
||||
M_DoctorCreated
|
||||
M_DoctorCreated,
|
||||
M_DoctorPhoto,
|
||||
M_DoctorPhotoLastUpdated,
|
||||
M_DoctorPhotoLastUpdatedUserID,
|
||||
M_DoctorAddressVillage,
|
||||
M_DoctorAddressRT,
|
||||
M_DoctorAddressRW
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
@@ -1180,7 +1285,13 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
NOW()
|
||||
NOW(),
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?
|
||||
)`
|
||||
|
||||
rst := tx.MustExec(query,
|
||||
@@ -1218,6 +1329,12 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
M_DoctorAddressCountry,
|
||||
"Y",
|
||||
"1",
|
||||
M_DoctorPhoto,
|
||||
M_DoctorPhotoLastUpdated,
|
||||
M_DoctorPhotoLastUpdatedUserID,
|
||||
M_DoctorAddressVillage,
|
||||
M_DoctorAddressRT,
|
||||
M_DoctorAddressRW,
|
||||
)
|
||||
insertedID, err := rst.LastInsertId()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user