Skip to content

Commit

Permalink
feat: adding two languages to response texts
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroVidalDev committed Apr 15, 2024
1 parent 12144ff commit 2b1d5d0
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class AccessRegisterController {

@PostMapping
@Transactional
public ResponseEntity create(@RequestBody @Valid AccessDataInput data){
AccessDataComplete accessDataComplete = service.validateCreation(data);
public ResponseEntity create(@RequestBody @Valid AccessDataInput data, @RequestHeader("Language") String language){
AccessDataComplete accessDataComplete = service.validateCreation(data, language);

return ResponseEntity.ok(accessDataComplete);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public class ConsumerController {

@PostMapping
@Transactional
public ResponseEntity create(@RequestBody @Valid ConsumerDataInput data){
var consumerDto = service.createVerify(data);
public ResponseEntity create(@RequestBody @Valid ConsumerDataInput data, @RequestHeader("Language") String language){
var consumerDto = service.createVerify(data, language);

return ResponseEntity.ok(consumerDto);
}

@PutMapping("/{ra}")
@Transactional
public ResponseEntity update(@PathVariable String ra, @RequestBody @Valid ConsumerDataEditInput data){
var consumerDto = service.updateVerify(data, ra);
public ResponseEntity update(@PathVariable String ra, @RequestBody @Valid ConsumerDataEditInput data, @RequestHeader("Language") String language){
var consumerDto = service.updateVerify(data, ra, language);

return ResponseEntity.ok(consumerDto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public class LocalController {

@PostMapping
@Transactional
public ResponseEntity create(@RequestBody @Valid LocalDataInput data){
var local = service.createVerify(data);
public ResponseEntity create(@RequestBody @Valid LocalDataInput data, @RequestHeader("Language") String language){
var local = service.createVerify(data, language);

return ResponseEntity.ok(local);
}

@PutMapping("/{name}")
@Transactional
public ResponseEntity update(@PathVariable String name, @RequestBody @Valid LocalDataEditInput data){
var local = service.updateVerify(data, name);
public ResponseEntity update(@PathVariable String name, @RequestBody @Valid LocalDataEditInput data, @RequestHeader("Language") String language){
var local = service.updateVerify(data, name, language);

return ResponseEntity.ok(local);
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/pedro/sphynx/domain/AccessService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,20 @@ public class AccessService {
@Autowired
private LocalRepository localRepository;

private ResourceBundle messages = ResourceBundle.getBundle("messages");
private ResourceBundle messages = ResourceBundle.getBundle("messagesEn");

public void defineMessagesLanguage(String language){
if(language.equals("pt-BR")){
messages = ResourceBundle.getBundle("messagesPt");
}
else if(language.equals("en-US")){
messages = ResourceBundle.getBundle("messagesEn");
}
}

public AccessDataComplete validateCreation(AccessDataInput data, String language){
defineMessagesLanguage(language);

public AccessDataComplete validateCreation(AccessDataInput data){
if(!consumerRepository.existsByTag(data.tag())){
throw new Validation(messages.getString("error.tagDontExists"));
}
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/pedro/sphynx/domain/ConsumerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ public class ConsumerService {
@Autowired
private PersonRepository personRepository;

private ResourceBundle messages = ResourceBundle.getBundle("messages");
private ResourceBundle messages = ResourceBundle.getBundle("messagesEn");

public void defineMessagesLanguage(String language){
if(language.equals("pt-BR")){
messages = ResourceBundle.getBundle("messagesPt");
}
else if(language.equals("en-US")){
messages = ResourceBundle.getBundle("messagesEn");
}
}

public ConsumerDataComplete createVerify(ConsumerDataInput data, String language){
defineMessagesLanguage(language);

public ConsumerDataComplete createVerify(ConsumerDataInput data){
if(consumerRepository.existsByPersonRa(data.ra())){
throw new Validation(messages.getString("error.raAlreadyExists"));
}
Expand All @@ -40,7 +51,9 @@ else if(!personRepository.existsByRa(data.ra())){
}
}

public ConsumerDataComplete updateVerify(ConsumerDataEditInput data, String ra){
public ConsumerDataComplete updateVerify(ConsumerDataEditInput data, String ra, String language){
defineMessagesLanguage(language);

if(consumerRepository.existsByPersonRa(ra)){
var consumer = consumerRepository.getReferenceByPersonRa(ra);
consumer.actualizeData(data);
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/com/pedro/sphynx/domain/LocalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ResourceBundle;

@Service
public class LocalService {

@Autowired
private LocalRepository repository;

public LocalDataComplete createVerify(LocalDataInput data){
private ResourceBundle messages = ResourceBundle.getBundle("messagesEn");

public void defineMessagesLanguage(String language){
if(language.equals("pt-BR")){
messages = ResourceBundle.getBundle("messagesPt");
}
else if(language.equals("en-US")){
messages = ResourceBundle.getBundle("messagesEn");
}
}

public LocalDataComplete createVerify(LocalDataInput data, String language){
defineMessagesLanguage(language);

if(!repository.existsByName(data.name()) || !repository.existsByMac(data.mac())){
Local local = new Local(data);

Expand All @@ -25,7 +40,9 @@ public LocalDataComplete createVerify(LocalDataInput data){
return null;
}

public LocalDataComplete updateVerify(LocalDataEditInput data, String name) {
public LocalDataComplete updateVerify(LocalDataEditInput data, String name, String language) {
defineMessagesLanguage(language);

if(!repository.existsByName(data.mac())){
Local local = repository.getReferenceByName(name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CorsFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
response.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, Access-Control-Allow-Origin, Access-Control-Allow-Credentials");
response.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, Access-Control-Allow-Origin, Access-Control-Allow-Credentials, Language");
if ("OPTIONS".equals(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions src/main/resources/messagesPt.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error.raDontExists=RA informado nao existe.
error.raAlreadyExists=RA informado ja existe no banco.
error.raDontExistsInPerson=RA informado nao existe na tabela de doscentes.

error.tagDontExists=TAG informada nao existe no banco.

error.localDontExists=Local informado nao existe no banco.

0 comments on commit 2b1d5d0

Please sign in to comment.