-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: List supported languages (#122)
* refactor(db): Create `archives` table * chore(lang): Create `languages` package * feat(db): Insert Java to the `languages` table * feat(lang): Add endpoint to list supported languages * test(lang): Add test to ensure languages are listed correctly
- Loading branch information
1 parent
5983288
commit e7d7539
Showing
16 changed files
with
302 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package integration | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestListSupportedLanguages(t *testing.T) { | ||
c := require.New(t) | ||
|
||
// Login as an student | ||
w, r := PrepareRequest("POST", "/api/v1/session/login", map[string]interface{}{ | ||
"email": registeredStudentEmail, | ||
"password": registeredStudentPass, | ||
}) | ||
router.ServeHTTP(w, r) | ||
cookie := w.Result().Cookies()[0] | ||
|
||
// Get the supported languages | ||
response, statusCode := GetSupportedLanguages(cookie) | ||
c.Equal(http.StatusOK, statusCode) | ||
|
||
// Check the response | ||
languages := response["languages"].([]interface{}) | ||
c.Greater(len(languages), 0) | ||
|
||
// Get all the languages names | ||
var languagesNames []string | ||
for _, language := range languages { | ||
languagesNames = append(languagesNames, language.(map[string]interface{})["name"].(string)) | ||
} | ||
|
||
// Check if the supported languages are included | ||
c.Contains(languagesNames, "Java") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package integration | ||
|
||
import "net/http" | ||
|
||
func GetSupportedLanguages(cookie *http.Cookie) (response map[string]interface{}, statusCode int) { | ||
w, r := PrepareRequest("GET", "/api/v1/languages", nil) | ||
r.AddCookie(cookie) | ||
router.ServeHTTP(w, r) | ||
return ParseJsonResponse(w.Body), w.Code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
meta { | ||
name: list-languages | ||
type: http | ||
seq: 1 | ||
} | ||
|
||
get { | ||
url: {{BASE_URL}}/languages | ||
body: none | ||
auth: none | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package application | ||
|
||
import ( | ||
"github.com/UPB-Code-Labs/main-api/src/languages/domain/definitions" | ||
"github.com/UPB-Code-Labs/main-api/src/languages/domain/entities" | ||
) | ||
|
||
type LanguageUseCases struct { | ||
LanguageRepository definitions.LanguagesRepository | ||
} | ||
|
||
func (useCases *LanguageUseCases) GetLanguages() ([]*entities.Language, error) { | ||
return useCases.LanguageRepository.GetAll() | ||
} | ||
|
||
func (useCases *LanguageUseCases) GetLanguageByUUID(uuid string) (*entities.Language, error) { | ||
return useCases.LanguageRepository.GetByUUID(uuid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package definitions | ||
|
||
import "github.com/UPB-Code-Labs/main-api/src/languages/domain/entities" | ||
|
||
type LanguagesRepository interface { | ||
GetAll() (languages []*entities.Language, err error) | ||
GetByUUID(uuid string) (language *entities.Language, err error) | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package entities | ||
|
||
type Language struct { | ||
UUID string `json:"uuid"` | ||
TemplateArchiveUUID string `json:"-"` | ||
Name string `json:"name"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package errors | ||
|
||
import "net/http" | ||
|
||
type LangNotFoundError struct{} | ||
|
||
func (err *LangNotFoundError) Error() string { | ||
return "Language not found" | ||
} | ||
|
||
func (err *LangNotFoundError) StatusCode() int { | ||
return http.StatusNotFound | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/UPB-Code-Labs/main-api/src/languages/application" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type LanguagesController struct { | ||
UseCases *application.LanguageUseCases | ||
} | ||
|
||
func (controller *LanguagesController) HandleGetLanguages(c *gin.Context) { | ||
languages, err := controller.UseCases.GetLanguages() | ||
if err != nil { | ||
c.Error(err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"languages": languages, | ||
}) | ||
} | ||
|
||
func (controller *LanguagesController) HandleDownloadLanguageTemplate(c *gin.Context) { | ||
c.Status(http.StatusNotImplemented) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package http | ||
|
||
import ( | ||
"github.com/UPB-Code-Labs/main-api/src/languages/application" | ||
"github.com/UPB-Code-Labs/main-api/src/languages/infrastructure/implementations" | ||
shared_infrastructure "github.com/UPB-Code-Labs/main-api/src/shared/infrastructure" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func StartLanguagesRoutes(g *gin.RouterGroup) { | ||
langGroup := g.Group("/languages") | ||
|
||
useCases := application.LanguageUseCases{ | ||
LanguageRepository: implementations.GetLanguagesRepositoryInstance(), | ||
} | ||
|
||
controllers := LanguagesController{ | ||
UseCases: &useCases, | ||
} | ||
|
||
langGroup.GET( | ||
"", | ||
shared_infrastructure.WithAuthenticationMiddleware(), | ||
shared_infrastructure.WithAuthorizationMiddleware([]string{"teacher", "student"}), | ||
controllers.HandleGetLanguages, | ||
) | ||
langGroup.GET( | ||
"/:language_uuid/template", | ||
shared_infrastructure.WithAuthenticationMiddleware(), | ||
shared_infrastructure.WithAuthorizationMiddleware([]string{"teacher", "student"}), | ||
controllers.HandleDownloadLanguageTemplate, | ||
) | ||
} |
Oops, something went wrong.