Squashed commit of the following:
commitd2ec8c0f07Author: mario <dev.mario@sismedika@gmail.com> Date: Thu May 15 15:42:33 2025 +0700 add: db tx commit and rollback implementation commit264435f67eAuthor: mario <dev.mario@sismedika@gmail.com> Date: Thu May 15 14:34:20 2025 +0700 fix: shortlink generation logic update/create commit047ab1937aAuthor: mario <dev.mario@sismedika@gmail.com> Date: Thu May 15 11:06:04 2025 +0700 fix: if multiple studies patient, show first study by default commitc13f834b92Author: mario <dev.mario@sismedika@gmail.com> Date: Thu May 15 09:46:32 2025 +0700 add: register and login with DB query AND some struct type correction commitdd4451c2a8Author: mario <dev.mario@sismedika@gmail.com> Date: Wed May 14 10:23:33 2025 +0700 new file structure & koneksi ke DB commit8289881df3Author: mario <dev.mario@sismedika@gmail.com> Date: Tue May 13 16:49:07 2025 +0700 edit: rm debug route commitdd784da232Author: mario <dev.mario@sismedika@gmail.com> Date: Tue May 13 15:44:11 2025 +0700 add: implement shortlink commit2687a761ccAuthor: mario <dev.mario@sismedika@gmail.com> Date: Tue May 13 11:47:19 2025 +0700 add new dummy doctor user commiteb67eaca46Author: mario <dev.mario@sismedika@gmail.com> Date: Tue May 13 11:46:28 2025 +0700 add: ref_doctor studylist filter commit0d4825d152Author: mario <dev.mario@sismedika@gmail.com> Date: Tue May 13 10:07:16 2025 +0700 edit study_iuids & accNum in patient jwt to array commit2d1f135fdaAuthor: mario <dev.mario@sismedika@gmail.com> Date: Tue May 13 09:52:45 2025 +0700 patient see their multiple studies commit13bb380f51Author: mario <dev.mario@sismedika@gmail.com> Date: Fri May 9 10:13:16 2025 +0700 add: cors handler route and readme commit6c9ab574ceAuthor: mario <dev.mario@sismedika@gmail.com> Date: Mon May 5 11:50:36 2025 +0700 add: login & token validation tapi belum connect ke DB commit297c9a6a01Author: mario <dev.mario@sismedika@gmail.com> Date: Mon Apr 28 15:37:02 2025 +0700 add readme.md commit9b8e0260f3Author: mario <dev.mario@sismedika@gmail.com> Date: Mon Apr 7 15:46:07 2025 +0700 connected-to-google commitf340bc5916Author: mario <dev.mario@sismedika.com> Date: Mon Apr 7 11:14:18 2025 +0700 init
This commit is contained in:
100
internal/api/repository/study.go
Normal file
100
internal/api/repository/study.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"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"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// DBStudy represents a study from the database
|
||||
type DBStudy struct {
|
||||
ID int `db:"id"`
|
||||
Study_PatientID string `db:"Study_PatientID"`
|
||||
StudyIUID string `db:"StudyIUID"`
|
||||
StudyAccessionNumber string `db:"StudyAccessionNumber"`
|
||||
StudyDate time.Time `db:"StudyDate"`
|
||||
StudyCreatedAt time.Time `db:"StudyCreatedAt"`
|
||||
StudyUpdatedAt time.Time `db:"StudyUpdatedAt"`
|
||||
}
|
||||
|
||||
// StudyRepository handles database operations related to studies
|
||||
type StudyRepository struct {
|
||||
*Repository
|
||||
}
|
||||
|
||||
// NewStudyRepository creates a new study repository
|
||||
func NewStudyRepository() *StudyRepository {
|
||||
return &StudyRepository{
|
||||
Repository: NewRepository(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetPatientStudies retrieves all studies for a patient
|
||||
func (r *StudyRepository) GetPatientStudies(patientID string) ([]string, []string, error) {
|
||||
var studies []DBStudy
|
||||
|
||||
query := `SELECT * FROM study WHERE Study_PatientID = ?`
|
||||
err := database.DB.Select(&studies, query, patientID)
|
||||
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("database error getting patient studies: %w", err)
|
||||
}
|
||||
|
||||
var studyUIDs []string
|
||||
var accessionNumbers []string
|
||||
|
||||
for _, study := range studies {
|
||||
studyUIDs = append(studyUIDs, study.StudyIUID)
|
||||
if study.StudyAccessionNumber != "" {
|
||||
accessionNumbers = append(accessionNumbers, study.StudyAccessionNumber)
|
||||
}
|
||||
}
|
||||
|
||||
return studyUIDs, accessionNumbers, nil
|
||||
}
|
||||
|
||||
// GetStudyByUID retrieves a study by its UID
|
||||
func (r *StudyRepository) GetStudyByUID(studyUID string) (*DBStudy, error) {
|
||||
var study DBStudy
|
||||
|
||||
query := `SELECT * FROM study WHERE StudyIUID = ?`
|
||||
err := database.DB.Get(&study, query, studyUID)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("database error getting study: %w", err)
|
||||
}
|
||||
|
||||
return &study, nil
|
||||
}
|
||||
|
||||
// CreateStudyTx creates a new study record for a patient within a transaction
|
||||
func (r *StudyRepository) CreateStudyTx(tx *sqlx.Tx, patientID string, study models.Study) error {
|
||||
// Parse study date if provided
|
||||
var studyDate *time.Time
|
||||
if study.StudyDate != "" {
|
||||
parsedTime, err := time.Parse("2006-01-02", study.StudyDate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid study date format: %w", err)
|
||||
}
|
||||
studyDate = &parsedTime
|
||||
}
|
||||
|
||||
query := `INSERT INTO study
|
||||
(Study_PatientID, StudyIUID, StudyAccessionNumber, StudyDate, StudyCreatedAt, StudyUpdatedAt)
|
||||
VALUES (?, ?, ?, ?, NOW(), NOW())`
|
||||
|
||||
_, err := tx.Exec(query,
|
||||
patientID,
|
||||
study.StudyInstanceUID,
|
||||
study.AccessionNumber,
|
||||
studyDate)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("database error creating study: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user