new file structure & koneksi ke DB
This commit is contained in:
62
internal/api/repository/patient.go
Normal file
62
internal/api/repository/patient.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/api/models"
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/database"
|
||||
)
|
||||
|
||||
// DBPatient represents a patient from the database
|
||||
type DBPatient struct {
|
||||
ID int `db:"id"`
|
||||
Patient_UsersID string `db:"Patient_UsersID"`
|
||||
PatientMedrec string `db:"PatientMedrec"`
|
||||
PatientName string `db:"PatientName"`
|
||||
PatientDoB time.Time `db:"PatientDoB"`
|
||||
PatientCreatedAt time.Time `db:"PatientCreatedAt"`
|
||||
PatientUpdatedAt time.Time `db:"PatientUpdatedAt"`
|
||||
}
|
||||
|
||||
// PatientRepository handles database operations related to patients
|
||||
type PatientRepository struct {
|
||||
*Repository
|
||||
}
|
||||
|
||||
// NewPatientRepository creates a new patient repository
|
||||
func NewPatientRepository() *PatientRepository {
|
||||
return &PatientRepository{
|
||||
Repository: NewRepository(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetPatientDetailsByUserID retrieves patient details for a user
|
||||
func (r *PatientRepository) GetPatientDetailsByUserID(userID string) (*models.PatientDetails, error) {
|
||||
var dbPatient DBPatient
|
||||
|
||||
query := `SELECT * FROM patient WHERE Patient_UsersID = ?`
|
||||
err := database.DB.Get(&dbPatient, query, userID)
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("database error getting patient details: %w", err)
|
||||
}
|
||||
|
||||
// Create StudyRepository to get patient studies
|
||||
studyRepo := NewStudyRepository()
|
||||
studyUIDs, accessionNumbers, err := studyRepo.GetPatientStudies(dbPatient.PatientMedrec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &models.PatientDetails{
|
||||
PatientID: dbPatient.PatientMedrec,
|
||||
PatientName: dbPatient.PatientName,
|
||||
StudyInstanceUIDs: studyUIDs,
|
||||
AccessionNumbers: accessionNumbers,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user