Skip to content

Commit

Permalink
feat(Multiple Languages): Add POST endpoint to save translations to a…
Browse files Browse the repository at this point in the history
… unit (#253) (#279)
  • Loading branch information
hirokiterashima authored Aug 5, 2024
1 parent d6f5cf5 commit edcb4b2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.wise.portal.presentation.web.controllers.author.project;

import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wise.portal.domain.project.impl.ProjectImpl;
import org.wise.portal.domain.user.User;
import org.wise.portal.service.project.ProjectService;
import org.wise.portal.service.project.translation.TranslateProjectService;
import org.wise.portal.service.user.UserService;

import com.fasterxml.jackson.databind.node.ObjectNode;

@Controller
@RequestMapping("/api/author/project/translate")
@Secured({ "ROLE_AUTHOR" })
public class TranslateProjectAPIController {

@Autowired
protected ProjectService projectService;

@Autowired
protected UserService userService;

@Autowired
protected TranslateProjectService translateProjectService;

@PostMapping("{projectId}/{locale}")
@ResponseBody
protected void saveTranslations(Authentication auth,
@PathVariable("projectId") ProjectImpl project, @PathVariable("locale") String locale,
@RequestBody ObjectNode translations) throws IOException {
User user = userService.retrieveUserByUsername(auth.getName());
if (projectService.canAuthorProject(project, user)) {
translateProjectService.saveTranslations(project, locale, translations.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.wise.portal.service.project.translation;

import java.io.IOException;
import org.wise.portal.domain.project.Project;

public interface TranslateProjectService {

public void saveTranslations(Project project, String locale, String translations)
throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.wise.portal.service.project.translation.impl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.wise.portal.domain.project.Project;
import org.wise.portal.service.project.translation.TranslateProjectService;

@Service
public class TranslateProjectServiceImpl implements TranslateProjectService {

@Autowired
private Environment appProperties;

public void saveTranslations(Project project, String locale, String translations)
throws IOException {
String translationFilePath = appProperties.getProperty("curriculum_base_dir")
+ project.getModulePath().replace("project.json", "translations." + locale + ".json");
Writer writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(new File(translationFilePath)), "UTF-8"));
writer.write(translations);
writer.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.wise.portal.presentation.web.controllers.author.project;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import org.easymock.EasyMockRunner;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wise.portal.presentation.web.controllers.APIControllerTest;
import org.wise.portal.service.project.translation.TranslateProjectService;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

@RunWith(EasyMockRunner.class)
public class TranslateProjectAPIControllerTest extends APIControllerTest {

@TestSubject
private TranslateProjectAPIController controller = new TranslateProjectAPIController();

@Mock
private TranslateProjectService translateProjectService;

@Test
public void saveTranslations_() throws Exception {
expect(userService.retrieveUserByUsername(TEACHER_USERNAME)).andReturn(teacher1);
expect(projectService.canAuthorProject(project1, teacher1)).andReturn(true);
translateProjectService.saveTranslations(project1, "es", "{}");
expectLastCall();
replay(projectService, translateProjectService, userService);
controller.saveTranslations(teacherAuth, project1, "es",
new ObjectNode(new JsonNodeFactory(false)));
verify(projectService, translateProjectService, userService);
}
}

0 comments on commit edcb4b2

Please sign in to comment.