Skip to content

Commit

Permalink
feat: update if exercise have testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
CheIby committed Sep 21, 2024
1 parent e1404a9 commit 4f632ca
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
Binary file modified main
Binary file not shown.
29 changes: 29 additions & 0 deletions models/lab_exercise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package models

import (
"encoding/json"
"time"

"github.com/google/uuid"
)

type LabExercise struct {
ExerciseID uuid.UUID `gorm:"type:varchar(36);primaryKey;column:exercise_id"`
ChapterID *uuid.UUID `gorm:"type:varchar(36);column:chapter_id"`
Level *string `gorm:"type:enum('0','1','2','3','4','5','6')"`
Name *string `gorm:"type:varchar(1024)"`
Content *string `gorm:"type:mediumtext"`
Testcase string `gorm:"type:enum('NO_INPUT','YES','UNDEFINED');not null;default:'NO_INPUT'"`
Sourcecode *string `gorm:"type:varchar(50)"`
FullMark int `gorm:"type:int;not null;default:10"`
AddedDate time.Time `gorm:"type:datetime;not null;default:CURRENT_TIMESTAMP"`
LastUpdate *time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP"`
UserDefinedConstraints *json.RawMessage `gorm:"type:json"`
SuggestedConstraints *json.RawMessage `gorm:"type:json"`
AddedBy *string `gorm:"type:varchar(40)"`
CreatedBy *uuid.UUID `gorm:"type:varchar(36)"`
}

func (LabExercise) TableName() string {
return "lab_exercises"
}
25 changes: 25 additions & 0 deletions repositories/lab_exercise_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package repositories

import (
"github.com/Project-IPCA/ipca-worker-go-v2/models"
"github.com/google/uuid"
"gorm.io/gorm"
)

type LabExerciseRepository struct {
DB *gorm.DB
}

func NewLabExerciseRePository(db *gorm.DB) *LabExerciseRepository {
return &LabExerciseRepository{
DB: db,
}
}

func (labExerciseRepo *LabExerciseRepository) GetLabExerciseById(labExercise *models.LabExercise,exerciseId uuid.UUID) {
labExerciseRepo.DB.Where("exercise_id",exerciseId).Find(labExercise)
}

func (labExerciseRepo *LabExerciseRepository) UpdateExerciseTestcaseEnum(labExercise *models.LabExercise,testcaseEnum string) {
labExerciseRepo.DB.Model(labExercise).Update("testcase" , testcaseEnum)
}
15 changes: 10 additions & 5 deletions service/update_testcase_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ func AddAndUpdateTestCase(channel *amqp.Channel, db_pool *gorm.DB, msg amqp.Deli

func compileCodeTestcase (db_pool *gorm.DB, msgBody models.ReciveMessage) error{
exerciseTestcaseRepo := repositories.NewExcerciseTestCaseRePository(db_pool)
labExerciseRepo := repositories.NewLabExerciseRePository(db_pool)

exerciseUuid,err := uuid.Parse(*msgBody.ExcerciseID)
if(err!=nil){
return utils.NewAppError(utils.ERROR_NAME.FUNCTION_ERROR,"failed to convert exercise uuid", err.Error())
}
var labExercise models.LabExercise
labExerciseRepo.GetLabExerciseById(&labExercise,exerciseUuid)
if(len(msgBody.TestCaseList)>0){
for i:=0; i<len(msgBody.TestCaseList); i++ {
testcaseUuid,err := uuid.Parse(msgBody.TestCaseList[i].TestCaseID)
if(err!=nil){
return utils.NewAppError(utils.ERROR_NAME.FUNCTION_ERROR,"failed to convert testcase uuid", err.Error())
}
exerciseUuid,err := uuid.Parse(*msgBody.ExcerciseID)
if(err!=nil){
return utils.NewAppError(utils.ERROR_NAME.FUNCTION_ERROR,"failed to convert exercise uuid", err.Error())
}
result, err := utils.RunPythonScript(msgBody.TestCaseList[i], msgBody.SourceCode)
if err != nil {
appErr, ok := err.(*utils.AppError)
Expand Down Expand Up @@ -101,8 +105,9 @@ func compileCodeTestcase (db_pool *gorm.DB, msgBody models.ReciveMessage) error{

err = exerciseTestcaseRepo.UpdateTestCase(&testCaseData,exerciseUuid)
if(err != nil){
utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR,"failed to write file", err.Error())
utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR,"failed to update testcase to db", err.Error())
}
labExerciseRepo.UpdateExerciseTestcaseEnum(&labExercise,"YES")
}
}
}
Expand Down

0 comments on commit 4f632ca

Please sign in to comment.