Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Multilingual Unit): API endpoint for unit translations #279

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}