Übersetzungen Server und Client

This commit is contained in:
2026-08-02 23:25:13 +02:00
parent a73de0b81f
commit 4df19f9acf
10 changed files with 526 additions and 29 deletions
@@ -0,0 +1,125 @@
package controller
import (
"encoding/json"
"io"
"log"
"net/http"
"scpq-editor-server/models"
"scpq-editor-server/services"
"strconv"
"github.com/gin-gonic/gin"
)
type TranslationsController struct {
TranslationsService *services.TranslationsService
}
func NewTranslationsController(TranslationsService *services.TranslationsService) *TranslationsController {
return &TranslationsController{
TranslationsService: TranslationsService,
}
}
func (lc TranslationsController) CreateTranslation(ctx *gin.Context) {
body, err := io.ReadAll(ctx.Request.Body)
if err != nil {
log.Println("Error while reading create Translation request body", err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
var Translation models.Translation
err = json.Unmarshal(body, &Translation)
if err != nil {
log.Println("Error while unmarshaling create runner request body", err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
response, responseErr := lc.TranslationsService.CreateTranslation(&Translation)
if responseErr != nil {
ctx.AbortWithStatusJSON(responseErr.Status, responseErr)
return
}
ctx.JSON(http.StatusOK, response)
}
func (lc TranslationsController) UpdateTranslation(ctx *gin.Context) {
body, err := io.ReadAll(ctx.Request.Body)
if err != nil {
log.Println("Error while reading update Translation request body", err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
var Translation models.Translation
err = json.Unmarshal(body, &Translation)
if err != nil {
log.Println("Error while unmarshaling update Translation request body", err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
responseErr := lc.TranslationsService.UpdateTranslation(&Translation)
if responseErr != nil {
ctx.AbortWithStatusJSON(responseErr.Status, responseErr)
return
}
ctx.Status(http.StatusNoContent)
}
func (lc TranslationsController) DeleteTranslation(ctx *gin.Context) {
TranslationId := ctx.Param("id")
lId, err := strconv.Atoi(TranslationId)
if err != nil {
log.Println("Error TranslationId not int", err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
responseErr := lc.TranslationsService.DeleteTranslation(lId)
if responseErr != nil {
ctx.JSON(responseErr.Status, responseErr)
return
}
ctx.Status(http.StatusNoContent)
}
func (lc TranslationsController) GetTranslation(ctx *gin.Context) {
TranslationId := ctx.Param("id")
lId, err := strconv.Atoi(TranslationId)
if err != nil {
log.Println("Error TranslationId not int", err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
response, responseErr := lc.TranslationsService.GetTranslation(lId)
if responseErr != nil {
ctx.JSON(responseErr.Status, responseErr)
return
}
ctx.JSON(http.StatusOK, response)
}
func (lc TranslationsController) GetAllTranslations(ctx *gin.Context) {
response, responseErr := lc.TranslationsService.GetAllTranslations()
if responseErr != nil {
ctx.JSON(responseErr.Status, responseErr)
return
}
ctx.JSON(http.StatusOK, response)
}
func (lc TranslationsController) GetAllTranslationsByLanguageId(ctx *gin.Context) {
languageId := ctx.Param("languageId")
lId, err := strconv.Atoi(languageId)
if err != nil {
log.Println("Error languageId not int", err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
response, responseErr := lc.TranslationsService.GetAllTranslationsByLanguageId(lId)
if responseErr != nil {
ctx.JSON(responseErr.Status, responseErr)
return
}
ctx.JSON(http.StatusOK, response)
}
@@ -0,0 +1,8 @@
package models
type Translation struct {
ID int `json:"id"`
LanguageID int `json:"language_id"`
Original string `json:"original"`
Translated string `json:"translated"`
}
@@ -0,0 +1,254 @@
package repositories
import (
"database/sql"
"net/http"
"scpq-editor-server/models"
"strconv"
)
type TranslationsRepository struct {
dbHandler *sql.DB
}
func NewTranslationsRepository(dbHandler *sql.DB) *TranslationsRepository {
return &TranslationsRepository{
dbHandler: dbHandler,
}
}
func (rr TranslationsRepository) CreateTranslation(Translation *models.Translation) (*models.Translation, *models.ResponseError) {
query := `INSERT INTO Translations(language_id, original, translated ) VALUES($1,$2,$3) RETURNING id`
rows, err := rr.dbHandler.Query(query, Translation.LanguageID, Translation.Original, Translation.Translated)
if err != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
defer rows.Close()
var idstr string
for rows.Next() {
err := rows.Scan(&idstr)
if err != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
}
if rows.Err() != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
TranslationId, err := strconv.Atoi(idstr)
if err != nil {
return nil, &models.ResponseError{
Message: "Translation id not int",
Status: http.StatusInternalServerError,
}
}
return &models.Translation{
ID: TranslationId,
LanguageID: Translation.LanguageID,
Original: Translation.Original,
Translated: Translation.Translated,
}, nil
}
func (rr TranslationsRepository) UpdateTranslation(Translation *models.Translation) *models.ResponseError {
query := `UPDATE Translations SET language_id=$1, original=$2, translated=$3 WHERE id=$4`
res, err := rr.dbHandler.Exec(query, Translation.LanguageID, Translation.Original, Translation.Translated, Translation.ID)
if err != nil {
return &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
rowsAffeced, err := res.RowsAffected()
if err != nil {
return &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
if rowsAffeced == 0 {
return &models.ResponseError{
Message: "No Translation found with the given id",
Status: http.StatusNotFound,
}
}
return nil
}
func (rr TranslationsRepository) DeleteTranslation(TranslationId int) *models.ResponseError {
query := `DELETE FROM Translations WHERE id=$1`
res, err := rr.dbHandler.Exec(query, TranslationId)
if err != nil {
return &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
rowsAffeced, err := res.RowsAffected()
if err != nil {
return &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
if rowsAffeced == 0 {
return &models.ResponseError{
Message: "No Translation found with the given id",
Status: http.StatusNotFound,
}
}
return nil
}
func (rr TranslationsRepository) GetTranslation(TranslationId int) (*models.Translation, *models.ResponseError) {
query := `SELECT id, language_id, original, translated FROM Translations WHERE id=$1`
rows, err := rr.dbHandler.Query(query, TranslationId)
if err != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
defer rows.Close()
var idstr, language_id_str, original, translated string
for rows.Next() {
err := rows.Scan(&idstr, &language_id_str, &original, &translated)
if err != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
}
if rows.Err() != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
languageId, err := strconv.Atoi(language_id_str)
if err != nil {
return nil, &models.ResponseError{
Message: "Translation id not int",
Status: http.StatusInternalServerError,
}
}
return &models.Translation{
ID: TranslationId,
LanguageID: languageId,
Original: original,
Translated: translated,
}, nil
}
func (rr TranslationsRepository) GetAllTranslations() ([]*models.Translation, *models.ResponseError) {
query := "SELECT id, language_id, original, translated FROM Translations"
rows, err := rr.dbHandler.Query(query)
if err != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
defer rows.Close()
Translations := make([]*models.Translation, 0)
var idstr, language_id_str, original, translated string
for rows.Next() {
err := rows.Scan(&idstr, &language_id_str, &original, &translated)
if err != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
id, err := strconv.Atoi(idstr)
if err != nil {
return nil, &models.ResponseError{
Message: "Translation id not int",
Status: http.StatusInternalServerError,
}
}
languageId, err := strconv.Atoi(language_id_str)
if err != nil {
return nil, &models.ResponseError{
Message: "Translation id not int",
Status: http.StatusInternalServerError,
}
}
Translation := &models.Translation{
ID: id,
LanguageID: languageId,
Original: original,
Translated: translated,
}
Translations = append(Translations, Translation)
}
if rows.Err() != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
return Translations, nil
}
func (rr TranslationsRepository) GetAllTranslationsByLanguageID(languageId int) ([]*models.Translation, *models.ResponseError) {
query := "SELECT id, original, translated FROM Translations WHERE language_id=$1"
rows, err := rr.dbHandler.Query(query, languageId)
if err != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
defer rows.Close()
Translations := make([]*models.Translation, 0)
var idstr, original, translated string
for rows.Next() {
err := rows.Scan(&idstr, &original, &translated)
if err != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
id, err := strconv.Atoi(idstr)
if err != nil {
return nil, &models.ResponseError{
Message: "Translation id not int",
Status: http.StatusInternalServerError,
}
}
Translation := &models.Translation{
ID: id,
LanguageID: languageId,
Original: original,
Translated: translated,
}
Translations = append(Translations, Translation)
}
if rows.Err() != nil {
return nil, &models.ResponseError{
Message: err.Error(),
Status: http.StatusInternalServerError,
}
}
return Translations, nil
}
@@ -29,18 +29,21 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
propertygroupsRepository := repositories.NewPropertiesgroupsRepository(dbHandler)
valuetypesRepository := repositories.NewValuetypesRepository(dbHandler)
valuecomplexitiesRepository := repositories.NewValuecomplexitiesRepository(dbHandler)
translationsRepository := repositories.NewTranslationsRepository(dbHandler)
// service initialisation:
languagesService := services.NewLanguagesService(languagesRepository)
propertygroupsService := services.NewPropertygroupsService(propertygroupsRepository)
valuetypesService := services.NewValuetypesService(valuetypesRepository)
valuecomplexitiesService := services.NewValuecomplexitiesService(valuecomplexitiesRepository)
translationsService := services.NewTranslationsService(translationsRepository)
// controller initialisation:
languageController := controller.NewLanguagesController(languagesService)
propertygroupsController := controller.NewPropertygroupsController(propertygroupsService)
valuetypesController := controller.NewValuetypesController(valuetypesService)
valuecomplexitiesController := controller.NewValuecomplexitiesController(valuecomplexitiesService)
translationsController := controller.NewTranslationsController(translationsService)
// Service API
router := gin.Default()
@@ -61,6 +64,14 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
router.GET("/propertygroup/:id", propertygroupsController.GetPropertygroup)
router.GET("/propertygroups", propertygroupsController.GetAllPropertygroups)
// translation centric routing
router.POST("/translation", translationsController.CreateTranslation)
router.PUT("/translation", translationsController.UpdateTranslation)
router.DELETE("/translation/:id", translationsController.DeleteTranslation)
router.GET("/translation/:id", translationsController.GetTranslation)
router.GET("/translations", translationsController.GetAllTranslations)
router.GET("/translations/:languageId", translationsController.GetAllTranslationsByLanguageId)
// valuetype centric routing
router.GET("/valuetype/:id", valuetypesController.GetValuetype)
router.GET("/valuetypes", valuetypesController.GetAllValuetypes)
@@ -0,0 +1,59 @@
package services
import (
"net/http"
"scpq-editor-server/models"
"scpq-editor-server/repositories"
)
type TranslationsService struct {
translationsRepository *repositories.TranslationsRepository
}
func NewTranslationsService(TranslationsRepository *repositories.TranslationsRepository) *TranslationsService {
return &TranslationsService{
translationsRepository: TranslationsRepository,
}
}
func (ls TranslationsService) CreateTranslation(translation *models.Translation) (*models.Translation, *models.ResponseError) {
responseErr := validateTranslation(translation)
if responseErr != nil {
return nil, responseErr
}
return ls.translationsRepository.CreateTranslation(translation)
}
func (ls TranslationsService) UpdateTranslation(Translation *models.Translation) *models.ResponseError {
responseErr := validateTranslation(Translation)
if responseErr != nil {
return responseErr
}
return ls.translationsRepository.UpdateTranslation(Translation)
}
func (ls TranslationsService) DeleteTranslation(TranslationId int) *models.ResponseError {
return ls.translationsRepository.DeleteTranslation(TranslationId)
}
func (ls TranslationsService) GetTranslation(TranslationId int) (*models.Translation, *models.ResponseError) {
return ls.translationsRepository.GetTranslation(TranslationId)
}
func (ls TranslationsService) GetAllTranslations() ([]*models.Translation, *models.ResponseError) {
return ls.translationsRepository.GetAllTranslations()
}
func (ls TranslationsService) GetAllTranslationsByLanguageId(languageId int) ([]*models.Translation, *models.ResponseError) {
return ls.translationsRepository.GetAllTranslationsByLanguageID(languageId)
}
func validateTranslation(Translation *models.Translation) *models.ResponseError {
if Translation.Original == "" {
return &models.ResponseError{
Message: "Invalid Translation original",
Status: http.StatusBadRequest,
}
}
return nil
}