Skip to content

Commit

Permalink
fixed error type for hrm, and added get/set connection
Browse files Browse the repository at this point in the history
  • Loading branch information
XIAOKAOBO committed Oct 17, 2023
1 parent 2dc8db5 commit da3052e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
6 changes: 3 additions & 3 deletions hrm/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"flag"
"fmt"

"github.com/CAS735-F23/macrun-teamvs_/hrm/adapters/repository"
"github.com/CAS735-F23/macrun-teamvs_/hrm/internal/adapters/handler"
"github.com/CAS735-F23/macrun-teamvs_/hrm/internal/adapters/repository"
"github.com/CAS735-F23/macrun-teamvs_/hrm/internal/core/services"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -38,9 +38,9 @@ func main() {
func InitRoutes() {
router := gin.Default()
handler := handler.NewHTTPHandler(*svc)
router.GET("/hrmss", handler.ListHRM)
router.GET("/hrms", handler.ListHRM)
router.POST("/hrm", handler.CreateHRM)
router.GET("/hrmss/:id", handler.GetHRM)
router.GET("/hrms/:id", handler.GetHRM)
// TODO: Implement when needed
// router.PUT("/player", handler.UpdatePlayer)
router.Run(":8000")
Expand Down
14 changes: 7 additions & 7 deletions hrm/internal/adapters/repository/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func NewMemoryRepository() *MemoryRepository {
}
}

func (r *MemoryRepository) List() ([]*domain.Player, error) {
func (r *MemoryRepository) List() ([]*domain.HRM, error) {
if r.hrms == nil {
// If r.players is nil, return an error or handle the case accordingly
return nil, fmt.Errorf("hrms map doesn't exit %w", ports.ErrorListPlayersFailed)
// If r.hrms is nil, return an error or handle the case accordingly
return nil, fmt.Errorf("hrms map doesn't exit %w", ports.ErrorListHRMSFailed)
}
hrms := make([]*domain.HRM, 0, len(r.hrms))

Expand All @@ -36,14 +36,14 @@ func (r *MemoryRepository) List() ([]*domain.Player, error) {
}

func (r *MemoryRepository) Create(hrm domain.HRM) error {
if r.hrms[] == nil {
if r.hrms == nil {
r.Lock()
r.hrms = make(map[uuid.UUID]domain.HRM)
r.Unlock()
}

if _, ok := r.hrms[hrm.GetID()]; ok {
return fmt.Errorf("player already exist: %w", ports.ErrorCreatePlayerFailed)
return fmt.Errorf("hrm already exist: %w", ports.ErrorCreateHRMFailed)
}
r.Lock()
r.hrms[hrm.GetID()] = hrm
Expand All @@ -55,12 +55,12 @@ func (mr *MemoryRepository) Get(pid uuid.UUID) (*domain.HRM, error) {
if hrm, ok := mr.hrms[pid]; ok {
return &hrm, nil
}
return &domain.HRM{}, ports.ErrorPlayerNotFound
return &domain.HRM{}, ports.ErrorHRMNotFound
}

func (r *MemoryRepository) Update(hrm domain.HRM) error {
if _, ok := r.hrms[hrm.GetID()]; ok {
return fmt.Errorf("player does not exist: %w", ports.ErrorUpdatePlayerFailed)
return fmt.Errorf("hrm does not exist: %w", ports.ErrorUpdateHRMFailed)
}
r.Lock()
r.hrms[hrm.GetID()] = hrm
Expand Down
13 changes: 11 additions & 2 deletions hrm/internal/core/domain/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package domain
import (
"errors"
"time"

"github.com/google/uuid"
)

Expand All @@ -26,7 +27,7 @@ type HRM struct {
UpdatedAt time.Time `json:"updated_at"`
}

// Getters and Setters for Player
// Getters and Setters for HRM
func (hrm *HRM) GetID() uuid.UUID {
return hrm.HRMId
}
Expand All @@ -35,6 +36,14 @@ func (hrm *HRM) SetID(id uuid.UUID) {
hrm.HRMId = id
}

func (hrm *HRM) getState() string {
return hrm.Status
}

func (hrm *HRM) initializeHRM() {
hrm.Status = "Connected"
}

func (hrm *HRM) getHRate() string {
return hrm.HRate
}
Expand All @@ -52,7 +61,7 @@ func NewHRM(hrm HRM) (HRM, error) {
HRMId: hrm.HRMId,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Status: "connected"
Status: "connected",
}
return hrmN, nil
}
8 changes: 4 additions & 4 deletions hrm/internal/core/ports/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

var (
ErrorListPlayersFailed = errors.New("failed to list players")
ErrorPlayerNotFound = errors.New("the player session not found in repository")
ErrorCreatePlayerFailed = errors.New("failed to add the player")
ErrorUpdatePlayerFailed = errors.New("failed to update player")
ErrorListHRMSFailed = errors.New("failed to list hrms")
ErrorHRMNotFound = errors.New("the hrm session not found in repository")
ErrorCreateHRMFailed = errors.New("failed to add the hrm")
ErrorUpdateHRMFailed = errors.New("failed to update hrm")
)

type HRMService interface {
Expand Down

0 comments on commit da3052e

Please sign in to comment.