Compare commits
2
Commits
3f452468d6
...
c2703c131d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c2703c131d | ||
|
|
74b637bcc9 |
@@ -126,20 +126,124 @@ func (r *HomeRepository) GetResearchAreas(ctx context.Context) ([]models.Researc
|
|||||||
return areas, rows.Err()
|
return areas, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *HomeRepository) normalizeResearchAreasOrder(ctx context.Context, tx *sql.Tx) error {
|
||||||
|
rows, err := tx.QueryContext(ctx, `SELECT id FROM research_areas ORDER BY display_order ASC, id ASC`)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("query areas for normalization: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var ids []int
|
||||||
|
for rows.Next() {
|
||||||
|
var id int
|
||||||
|
if err := rows.Scan(&id); err != nil {
|
||||||
|
return fmt.Errorf("scan area id: %w", err)
|
||||||
|
}
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx, id := range ids {
|
||||||
|
newOrder := idx + 1
|
||||||
|
_, err := tx.ExecContext(ctx, `UPDATE research_areas SET display_order = ? WHERE id = ?`, newOrder, id)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("update area %d to order %d: %w", id, newOrder, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *HomeRepository) CreateResearchArea(ctx context.Context, a models.ResearchArea) (int, error) {
|
func (r *HomeRepository) CreateResearchArea(ctx context.Context, a models.ResearchArea) (int, error) {
|
||||||
res, err := r.db.ExecContext(ctx, `
|
tx, err := r.db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("begin tx: %w", err)
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
if a.DisplayOrder > 0 {
|
||||||
|
// Shift existing research areas with display_order >= target order by +1
|
||||||
|
_, err = tx.ExecContext(ctx, `
|
||||||
|
UPDATE research_areas
|
||||||
|
SET display_order = display_order + 1
|
||||||
|
WHERE display_order >= ?
|
||||||
|
`, a.DisplayOrder)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("shift research areas display_order: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var maxOrder sql.NullInt64
|
||||||
|
_ = tx.QueryRowContext(ctx, `SELECT MAX(display_order) FROM research_areas`).Scan(&maxOrder)
|
||||||
|
if maxOrder.Valid {
|
||||||
|
a.DisplayOrder = int(maxOrder.Int64) + 1
|
||||||
|
} else {
|
||||||
|
a.DisplayOrder = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := tx.ExecContext(ctx, `
|
||||||
INSERT INTO research_areas (name_en, name_kr, display_order)
|
INSERT INTO research_areas (name_en, name_kr, display_order)
|
||||||
VALUES (?, ?, ?)
|
VALUES (?, ?, ?)
|
||||||
`, a.NameEn, a.NameKr, a.DisplayOrder)
|
`, a.NameEn, a.NameKr, a.DisplayOrder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("insert research area: %w", err)
|
return 0, fmt.Errorf("insert research area: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := res.LastInsertId()
|
id, err := res.LastInsertId()
|
||||||
return int(id), err
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("get inserted area id: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.normalizeResearchAreasOrder(ctx, tx); err != nil {
|
||||||
|
return 0, fmt.Errorf("normalize display_order: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
return 0, fmt.Errorf("commit tx: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return int(id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *HomeRepository) UpdateResearchArea(ctx context.Context, id int, a models.ResearchArea) error {
|
func (r *HomeRepository) UpdateResearchArea(ctx context.Context, id int, a models.ResearchArea) error {
|
||||||
res, err := r.db.ExecContext(ctx, `
|
tx, err := r.db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("begin tx: %w", err)
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
var oldOrder int
|
||||||
|
err = tx.QueryRowContext(ctx, `SELECT display_order FROM research_areas WHERE id = ?`, id).Scan(&oldOrder)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return sql.ErrNoRows
|
||||||
|
}
|
||||||
|
return fmt.Errorf("get old display_order: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.DisplayOrder > 0 && a.DisplayOrder != oldOrder {
|
||||||
|
if oldOrder < a.DisplayOrder {
|
||||||
|
_, err = tx.ExecContext(ctx, `
|
||||||
|
UPDATE research_areas
|
||||||
|
SET display_order = display_order - 1
|
||||||
|
WHERE display_order > ? AND display_order <= ? AND id != ?
|
||||||
|
`, oldOrder, a.DisplayOrder, id)
|
||||||
|
} else {
|
||||||
|
_, err = tx.ExecContext(ctx, `
|
||||||
|
UPDATE research_areas
|
||||||
|
SET display_order = display_order + 1
|
||||||
|
WHERE display_order >= ? AND display_order < ? AND id != ?
|
||||||
|
`, a.DisplayOrder, oldOrder, id)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("shift research areas display_order on update: %w", err)
|
||||||
|
}
|
||||||
|
} else if a.DisplayOrder <= 0 {
|
||||||
|
a.DisplayOrder = oldOrder
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := tx.ExecContext(ctx, `
|
||||||
UPDATE research_areas
|
UPDATE research_areas
|
||||||
SET name_en = ?, name_kr = ?, display_order = ?
|
SET name_en = ?, name_kr = ?, display_order = ?
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
@@ -151,11 +255,22 @@ func (r *HomeRepository) UpdateResearchArea(ctx context.Context, id int, a model
|
|||||||
if err == nil && rows == 0 {
|
if err == nil && rows == 0 {
|
||||||
return sql.ErrNoRows
|
return sql.ErrNoRows
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
|
if err := r.normalizeResearchAreasOrder(ctx, tx); err != nil {
|
||||||
|
return fmt.Errorf("normalize display_order on update: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *HomeRepository) DeleteResearchArea(ctx context.Context, id int) error {
|
func (r *HomeRepository) DeleteResearchArea(ctx context.Context, id int) error {
|
||||||
res, err := r.db.ExecContext(ctx, `DELETE FROM research_areas WHERE id = ?`, id)
|
tx, err := r.db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("begin tx: %w", err)
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
res, err := tx.ExecContext(ctx, `DELETE FROM research_areas WHERE id = ?`, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("delete research area: %w", err)
|
return fmt.Errorf("delete research area: %w", err)
|
||||||
}
|
}
|
||||||
@@ -163,7 +278,12 @@ func (r *HomeRepository) DeleteResearchArea(ctx context.Context, id int) error {
|
|||||||
if err == nil && rows == 0 {
|
if err == nil && rows == 0 {
|
||||||
return sql.ErrNoRows
|
return sql.ErrNoRows
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
|
if err := r.normalizeResearchAreasOrder(ctx, tx); err != nil {
|
||||||
|
return fmt.Errorf("normalize display_order on delete: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *HomeRepository) GetStatsSummary(ctx context.Context) (*models.StatsSummary, error) {
|
func (r *HomeRepository) GetStatsSummary(ctx context.Context) (*models.StatsSummary, error) {
|
||||||
|
|||||||
@@ -241,6 +241,10 @@ type APIResponse[T any] struct {
|
|||||||
} `json:"error"`
|
} `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ptrString(s string) *string {
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
|
||||||
func doRequest(r http.Handler, method, target string, body any, token string) *httptest.ResponseRecorder {
|
func doRequest(r http.Handler, method, target string, body any, token string) *httptest.ResponseRecorder {
|
||||||
var reqBody []byte
|
var reqBody []byte
|
||||||
if body != nil {
|
if body != nil {
|
||||||
@@ -349,6 +353,87 @@ func TestResearchProjectsCRUD(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusNoContent, wDelete.Code)
|
assert.Equal(t, http.StatusNoContent, wDelete.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResearchAreasDisplayOrderShift(t *testing.T) {
|
||||||
|
db := setupTestDB(t)
|
||||||
|
defer db.Close()
|
||||||
|
r := router.SetupRouter(db, testAdminToken)
|
||||||
|
|
||||||
|
// 1. Fetch initial research areas
|
||||||
|
wInit := doRequest(r, "GET", "/api/v1/research-areas", nil, "")
|
||||||
|
assert.Equal(t, http.StatusOK, wInit.Code)
|
||||||
|
var initResp APIResponse[[]models.ResearchArea]
|
||||||
|
require.NoError(t, json.Unmarshal(wInit.Body.Bytes(), &initResp))
|
||||||
|
initAreas := initResp.Data
|
||||||
|
initCount := len(initAreas)
|
||||||
|
require.NotEmpty(t, initAreas)
|
||||||
|
|
||||||
|
// Verify initial areas are contiguous 1..N
|
||||||
|
for idx, a := range initAreas {
|
||||||
|
assert.Equal(t, idx+1, a.DisplayOrder)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Create at target display_order = 2 (should shift existing 2..N to 3..N+1 and normalize 1..N+1)
|
||||||
|
newArea := models.ResearchArea{
|
||||||
|
NameEn: "Quantum Internet Protocols",
|
||||||
|
NameKr: ptrString("양자 인터넷 프로토콜"),
|
||||||
|
DisplayOrder: 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
wCreate := doRequest(r, "POST", "/api/v1/research-areas", newArea, testAdminToken)
|
||||||
|
assert.Equal(t, http.StatusCreated, wCreate.Code)
|
||||||
|
var createResp APIResponse[models.ResearchArea]
|
||||||
|
require.NoError(t, json.Unmarshal(wCreate.Body.Bytes(), &createResp))
|
||||||
|
createdID := createResp.Data.ID
|
||||||
|
assert.Equal(t, 2, createResp.Data.DisplayOrder)
|
||||||
|
|
||||||
|
// Fetch updated list and assert display_order shifted correctly and gapless
|
||||||
|
wAfterCreate := doRequest(r, "GET", "/api/v1/research-areas", nil, "")
|
||||||
|
assert.Equal(t, http.StatusOK, wAfterCreate.Code)
|
||||||
|
var afterCreateResp APIResponse[[]models.ResearchArea]
|
||||||
|
require.NoError(t, json.Unmarshal(wAfterCreate.Body.Bytes(), &afterCreateResp))
|
||||||
|
afterCreateAreas := afterCreateResp.Data
|
||||||
|
|
||||||
|
assert.Equal(t, initCount+1, len(afterCreateAreas))
|
||||||
|
assert.Equal(t, "Quantum Internet Protocols", afterCreateAreas[1].NameEn)
|
||||||
|
for idx, a := range afterCreateAreas {
|
||||||
|
assert.Equal(t, idx+1, a.DisplayOrder, "Item %d should have display_order %d", idx, idx+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Update display_order of the created item from position 2 to position 5
|
||||||
|
updateArea := models.ResearchArea{
|
||||||
|
NameEn: "Quantum Internet Protocols Updated",
|
||||||
|
DisplayOrder: 5,
|
||||||
|
}
|
||||||
|
wUpdate := doRequest(r, "PUT", "/api/v1/research-areas/"+strconvItoa(createdID), updateArea, testAdminToken)
|
||||||
|
assert.Equal(t, http.StatusOK, wUpdate.Code)
|
||||||
|
|
||||||
|
wAfterUpdate := doRequest(r, "GET", "/api/v1/research-areas", nil, "")
|
||||||
|
var afterUpdateResp APIResponse[[]models.ResearchArea]
|
||||||
|
require.NoError(t, json.Unmarshal(wAfterUpdate.Body.Bytes(), &afterUpdateResp))
|
||||||
|
afterUpdateAreas := afterUpdateResp.Data
|
||||||
|
|
||||||
|
assert.Equal(t, initCount+1, len(afterUpdateAreas))
|
||||||
|
assert.Equal(t, "Quantum Internet Protocols Updated", afterUpdateAreas[4].NameEn)
|
||||||
|
assert.Equal(t, 5, afterUpdateAreas[4].DisplayOrder)
|
||||||
|
for idx, a := range afterUpdateAreas {
|
||||||
|
assert.Equal(t, idx+1, a.DisplayOrder, "Item %d should have contiguous display_order %d after update", idx, idx+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Delete the item and verify gapless 1..N normalization back to initCount
|
||||||
|
wDel := doRequest(r, "DELETE", "/api/v1/research-areas/"+strconvItoa(createdID), nil, testAdminToken)
|
||||||
|
assert.Equal(t, http.StatusNoContent, wDel.Code)
|
||||||
|
|
||||||
|
wAfterDel := doRequest(r, "GET", "/api/v1/research-areas", nil, "")
|
||||||
|
var afterDelResp APIResponse[[]models.ResearchArea]
|
||||||
|
require.NoError(t, json.Unmarshal(wAfterDel.Body.Bytes(), &afterDelResp))
|
||||||
|
afterDelAreas := afterDelResp.Data
|
||||||
|
|
||||||
|
assert.Equal(t, initCount, len(afterDelAreas))
|
||||||
|
for idx, a := range afterDelAreas {
|
||||||
|
assert.Equal(t, idx+1, a.DisplayOrder, "Item %d should have contiguous display_order %d after delete", idx, idx+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMembersAndAlumniCRUD(t *testing.T) {
|
func TestMembersAndAlumniCRUD(t *testing.T) {
|
||||||
db := setupTestDB(t)
|
db := setupTestDB(t)
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|||||||
Reference in New Issue
Block a user