Skip to content

Commit c5f7831

Browse files
committed
fix: implement missing repository and service methods for complete API functionality
- Added missing methods to StaffRepository: Update, Delete, FindByBarcode - Added missing methods to SettingRepository: FindByKey, Create, Delete - Added missing methods to ItemRepository: FindByBarcode - Added corresponding service layer methods for all repositories - Fixed template loading pattern from web/templates/**/* to web/templates/* - Ensured complete API compatibility with original Kotlin version All build errors resolved and application starts successfully with full API coverage.
1 parent 9135198 commit c5f7831

3 files changed

Lines changed: 169 additions & 1 deletion

File tree

cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func main() {
4343
router := gin.Default()
4444

4545
// Load HTML templates
46-
router.LoadHTMLGlob("web/templates/**/*")
46+
router.LoadHTMLGlob("web/templates/*")
4747

4848
// Serve static files
4949
router.Static("/static", "./web/static")

internal/repository/repositories.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ func (r *ItemRepository) Delete(id int) error {
115115
return err
116116
}
117117

118+
func (r *ItemRepository) FindByBarcode(barcode string) (*models.Item, error) {
119+
query := `SELECT id, itemId, name, price, stock, isDeleted, createdAt, updatedAt
120+
FROM item WHERE itemId = ? AND isDeleted = 0`
121+
122+
item := &models.Item{}
123+
err := r.db.QueryRow(query, barcode).Scan(&item.ID, &item.ItemID, &item.Name,
124+
&item.Price, &item.Stock, &item.IsDeleted, &item.CreatedAt, &item.UpdatedAt)
125+
126+
if err == sql.ErrNoRows {
127+
return nil, fmt.Errorf("item not found")
128+
}
129+
if err != nil {
130+
return nil, err
131+
}
132+
return item, nil
133+
}
134+
118135
// StoreRepository handles store data access
119136
type StoreRepository struct {
120137
db *sql.DB
@@ -241,6 +258,40 @@ func (r *StaffRepository) Create(staff *models.Staff) error {
241258
return nil
242259
}
243260

261+
func (r *StaffRepository) Update(staff *models.Staff) error {
262+
query := `UPDATE staff SET name = ?, updatedAt = ? WHERE id = ?`
263+
264+
now := time.Now()
265+
_, err := r.db.Exec(query, staff.Name, now, staff.ID)
266+
if err != nil {
267+
return err
268+
}
269+
staff.UpdatedAt = now
270+
return nil
271+
}
272+
273+
func (r *StaffRepository) Delete(id int) error {
274+
query := `DELETE FROM staff WHERE id = ?`
275+
_, err := r.db.Exec(query, id)
276+
return err
277+
}
278+
279+
func (r *StaffRepository) FindByBarcode(barcode string) (*models.Staff, error) {
280+
query := `SELECT id, staffId, name, createdAt, updatedAt FROM staff WHERE staffId = ?`
281+
282+
staff := &models.Staff{}
283+
err := r.db.QueryRow(query, barcode).Scan(&staff.ID, &staff.StaffID,
284+
&staff.Name, &staff.CreatedAt, &staff.UpdatedAt)
285+
286+
if err == sql.ErrNoRows {
287+
return nil, fmt.Errorf("staff not found")
288+
}
289+
if err != nil {
290+
return nil, err
291+
}
292+
return staff, nil
293+
}
294+
244295
// SaleRepository handles sale data access
245296
type SaleRepository struct {
246297
db *sql.DB
@@ -358,3 +409,45 @@ func (r *SettingRepository) Update(key, value string) error {
358409
_, err := r.db.Exec(query, value, time.Now(), key)
359410
return err
360411
}
412+
413+
func (r *SettingRepository) FindByKey(key string) (*models.Setting, error) {
414+
query := `SELECT id, key, value, type, description, createdAt, updatedAt FROM setting WHERE key = ?`
415+
416+
setting := &models.Setting{}
417+
err := r.db.QueryRow(query, key).Scan(&setting.ID, &setting.Key, &setting.Value,
418+
&setting.Type, &setting.Description, &setting.CreatedAt, &setting.UpdatedAt)
419+
420+
if err == sql.ErrNoRows {
421+
return nil, fmt.Errorf("setting not found")
422+
}
423+
if err != nil {
424+
return nil, err
425+
}
426+
return setting, nil
427+
}
428+
429+
func (r *SettingRepository) Create(setting *models.Setting) error {
430+
query := `INSERT INTO setting (key, value, type, description, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?)`
431+
432+
now := time.Now()
433+
result, err := r.db.Exec(query, setting.Key, setting.Value, setting.Type, setting.Description, now, now)
434+
if err != nil {
435+
return err
436+
}
437+
438+
id, err := result.LastInsertId()
439+
if err != nil {
440+
return err
441+
}
442+
setting.ID = int(id)
443+
setting.CreatedAt = now
444+
setting.UpdatedAt = now
445+
446+
return nil
447+
}
448+
449+
func (r *SettingRepository) Delete(key string) error {
450+
query := `DELETE FROM setting WHERE key = ?`
451+
_, err := r.db.Exec(query, key)
452+
return err
453+
}

internal/service/services.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ func (s *ItemService) DeleteItem(id int) error {
8181
return s.repo.Delete(id)
8282
}
8383

84+
func (s *ItemService) GetItemByID(id int) (*models.Item, error) {
85+
return s.repo.FindByID(id)
86+
}
87+
88+
func (s *ItemService) GetItemByBarcode(barcode string) (*models.Item, error) {
89+
return s.repo.FindByBarcode(barcode)
90+
}
91+
8492
func (s *ItemService) generateItemID() string {
8593
// Generate unique item ID with prefix
8694
return fmt.Sprintf("ITEM-%s", uuid.New().String()[:8])
@@ -144,6 +152,49 @@ func (s *StaffService) CreateStaff(staff *models.Staff) error {
144152
return s.repo.Create(staff)
145153
}
146154

155+
func (s *StaffService) GetAllStaff() ([]*models.Staff, error) {
156+
return s.repo.FindAll()
157+
}
158+
159+
func (s *StaffService) GetStaffByBarcode(barcode string) (*models.Staff, error) {
160+
return s.repo.FindByBarcode(barcode)
161+
}
162+
163+
func (s *StaffService) UpdateStaff(staff *models.Staff) error {
164+
// Validate
165+
if staff.Name == "" {
166+
return fmt.Errorf("staff name is required")
167+
}
168+
169+
return s.repo.Update(staff)
170+
}
171+
172+
func (s *StaffService) UpdateStaffByBarcode(barcode string, staff *models.Staff) error {
173+
// Find existing staff by barcode
174+
existingStaff, err := s.repo.FindByBarcode(barcode)
175+
if err != nil {
176+
return err
177+
}
178+
179+
// Update fields
180+
existingStaff.Name = staff.Name
181+
return s.repo.Update(existingStaff)
182+
}
183+
184+
func (s *StaffService) DeleteStaff(id int) error {
185+
return s.repo.Delete(id)
186+
}
187+
188+
func (s *StaffService) DeleteStaffByBarcode(barcode string) error {
189+
// Find staff by barcode first
190+
staff, err := s.repo.FindByBarcode(barcode)
191+
if err != nil {
192+
return err
193+
}
194+
195+
return s.repo.Delete(staff.ID)
196+
}
197+
147198
func (s *StaffService) generateStaffID() string {
148199
return fmt.Sprintf("STAFF-%s", uuid.New().String()[:8])
149200
}
@@ -220,6 +271,22 @@ func (s *SettingService) GetAllSettings() ([]*models.Setting, error) {
220271
return s.repo.FindAll()
221272
}
222273

274+
func (s *SettingService) GetSetting(key string) (*models.Setting, error) {
275+
return s.repo.FindByKey(key)
276+
}
277+
278+
func (s *SettingService) CreateSetting(setting *models.Setting) error {
279+
// Validate
280+
if setting.Key == "" {
281+
return fmt.Errorf("setting key is required")
282+
}
283+
if setting.Value == "" {
284+
return fmt.Errorf("setting value is required")
285+
}
286+
287+
return s.repo.Create(setting)
288+
}
289+
223290
func (s *SettingService) UpdateSetting(key, value string) error {
224291
if key == "" {
225292
return fmt.Errorf("setting key is required")
@@ -230,3 +297,11 @@ func (s *SettingService) UpdateSetting(key, value string) error {
230297

231298
return s.repo.Update(key, value)
232299
}
300+
301+
func (s *SettingService) DeleteSetting(key string) error {
302+
if key == "" {
303+
return fmt.Errorf("setting key is required")
304+
}
305+
306+
return s.repo.Delete(key)
307+
}

0 commit comments

Comments
 (0)