Skip to content

Commit

Permalink
Merge branch 'master' of github-personal.com:najamuslim/go-health-record
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhanramadhana committed May 19, 2024
2 parents 62e73f4 + a50f849 commit db5b99e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func main() {
itAuthorized.GET("/v1/user", nurseHandler.GetUsers)
itAuthorized.PUT("/v1/user/nurse/:userId", nurseHandler.UpdateNurse)
itAuthorized.DELETE("/v1/user/nurse/:userId", nurseHandler.DeleteNurse)
itAuthorized.PUT("/v1/user/nurse/:userId/access", nurseHandler.AddAccess)
itAuthorized.POST("/v1/user/nurse/:userId/access", nurseHandler.AddAccess)

// Manage medical records
authorized.POST("/v1/medical/patient", patientHandler.CreatePatient)
Expand Down
6 changes: 6 additions & 0 deletions src/handler/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ func (h *AuthHandler) LoginNurse(c *gin.Context) {
return
}

if request.Nip == 0 {
log.Println("Register bad request > invalid IdentityCardScanImg")
c.JSON(400, gin.H{"status": "bad request", "message": "invalid IdentityCardScanImg"})
return
}

nStr := strconv.FormatInt(request.Nip, 10)
if !strings.HasPrefix(nStr, "303") {
c.JSON(404, gin.H{"status": "bad request", "message": "user not found"})
Expand Down
15 changes: 15 additions & 0 deletions src/handler/nurse_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewNurseHandler(iNurseUsecase usecase.NurseUsecaseInterface) NurseHandlerIn
return &NurseHandler{iNurseUsecase}
}


func (h *NurseHandler) RegisterNurse(c *gin.Context) {
var request dto.RequestCreateNurse
err := c.ShouldBindJSON(&request)
Expand All @@ -30,6 +31,14 @@ func (h *NurseHandler) RegisterNurse(c *gin.Context) {
c.JSON(400, gin.H{"status": "bad request", "message": err})
return
}

fmt.Println("request.IdentityCardScanImg>>>>>>>>>>>>>>", request.IdentityCardScanImg)
fmt.Println("isValidURL(request.IdentityCardScanImg)>>>>>>>>>>>>>>", isValidURL(request.IdentityCardScanImg))
if !isValidURL(request.IdentityCardScanImg) {
log.Println("Register bad request > invalid IdentityCardScanImg", err)
c.JSON(400, gin.H{"status": "bad request", "message": "invalid IdentityCardScanImg"})
return
}

// Validate request payload
err = ValidateRegisterNurseRequest(request.Nip, request.Name)
Expand All @@ -39,6 +48,12 @@ func (h *NurseHandler) RegisterNurse(c *gin.Context) {
return
}

if request.IdentityCardScanImg == "" {
log.Println("Register bad request > invalid IdentityCardScanImg")
c.JSON(400, gin.H{"status": "bad request", "message": "invalid IdentityCardScanImg"})
return
}

// Check if email already exists
exists, _ := h.iNurseUsecase.GetNurseByNIP(request.Nip)
if exists {
Expand Down
17 changes: 12 additions & 5 deletions src/handler/patient_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -297,10 +298,16 @@ func validateGender(gender string) bool {
return gender == "male" || gender == "female"
}

func isValidURL(str string) bool {
u, err := url.Parse(str)
if err != nil || u.Scheme == "" || (u.Scheme != "http" && u.Scheme != "https") {
func isValidURL(input string) bool {
// Parse the URL to check for basic URL structure
parsedURL, err := url.ParseRequestURI(input)
if err != nil {
return false
}
return true
}

// Regex to check for a valid domain in the URL
domainRegex := regexp.MustCompile(`^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)

// Check if the host part of the URL matches the domain regex
return domainRegex.MatchString(parsedURL.Host)
}
3 changes: 2 additions & 1 deletion src/repository/nurse_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"time"

"github.com/docker/distribution/uuid"
"golang.org/x/crypto/bcrypt"
)

Expand Down Expand Up @@ -40,7 +41,7 @@ func (repo *NurseRepository) CreateNurse(ctx context.Context, nurse dto.RequestC
// Prepare the SQL query to insert the new nurse with the hashed password
const query = `INSERT INTO users (user_id, nip, name, role, identity_card_scan_img, password, created_at) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING user_id`
var userId string
err = repo.db.QueryRowContext(ctx, query, time.Now().UTC().Format("2006-01-02 15:04:05") + strconv.Itoa(randomInt(1, 100000)), nurse.Nip, nurse.Name, "nurse", nurse.IdentityCardScanImg, hashedPassword, time.Now()).Scan(&userId)
err = repo.db.QueryRowContext(ctx, query, uuid.Generate().String(), nurse.Nip, nurse.Name, "nurse", nurse.IdentityCardScanImg, hashedPassword, time.Now()).Scan(&userId)
if err != nil {
return "", err
}
Expand Down
5 changes: 3 additions & 2 deletions src/repository/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"fmt"
"health-record/model/database"
"math/rand"
"strconv"
"time"

"github.com/docker/distribution/uuid"
)

type UserRepository struct {
Expand Down Expand Up @@ -40,7 +41,7 @@ func (r *UserRepository) CreateUser(ctx context.Context, data database.User) (er
_, err = r.db.ExecContext(
ctx,
query,
time.Now().UTC().Format("2006-01-02 15:04:05") + strconv.Itoa(randomInt(1, 100000)),
uuid.Generate().String(),
data.Nip,
data.Name,
data.Password,
Expand Down

0 comments on commit db5b99e

Please sign in to comment.