Skip to content
Open
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
10 changes: 10 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
IP_HOST=10.43.100.19
PORT_HOST=4444
PORT_HOST_QA=24444
USERNAME_DATABASE=root
PASSWORD_DATABASE=mysql_deployment
PASSWORD_DATABASE_QA=mysql_qa
URL_EUREKA=10.43.101.226:8761
URL_EUREKA_QA=10.43.101.226:28761
URL_DATABASE=10.43.100.12:13306
URL_DATABASE_QA=10.43.100.12:23306
8 changes: 8 additions & 0 deletions .env_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
IP_HOST=
PORT_HOST=
PORT_HOST_QA=
USERNAME_DATABASE=
PASSWORD_DATABASE=
PASSWORD_DATABASE_QA=
URL_EUREKA=
URL_EUREKA_QA=
4 changes: 3 additions & 1 deletion compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ services:
IP_HOST: ${IP_HOST}
PORT_HOST: ${PORT_HOST}
URL_DATABASE: ${URL_DATABASE}
restart: always


event-microservice-qa:
build: .
container_name: event-microservice-qa
Expand All @@ -29,3 +29,5 @@ services:
IP_HOST: ${IP_HOST}
PORT_HOST: ${PORT_HOST_QA}
URL_DATABASE: ${URL_DATABASE_QA}
restart: always

24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,31 @@
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.18.2</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite-engine -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>1.10.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite-api -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>1.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
import com.ekomodatech.festivanow.event.repository.EventRepository;
import com.ekomodatech.festivanow.event.repository.LogisticRepository;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.web.bind.annotation.RequestHeader;


@RestController
@RequestMapping("/event")
public class EventController {
Expand All @@ -33,43 +42,109 @@ public class EventController {
private LogisticRepository logisticRepository;

@GetMapping("/{id}")
@CrossOrigin(origins = "http://localhost:4200")
public ResponseEntity<Event> findEvent(@PathVariable Long id) {
try {
Event event = eventRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Event not found"));
return ResponseEntity.ok(event);
} catch (ResponseStatusException ex) {
throw ex;
} catch (Exception ex) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", ex);
public ResponseEntity<Event> findEvent(@PathVariable Long id, @RequestHeader(value = "Authorization", required = false) String authorizationHeader) {
try {
Event event = eventRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Event not found"));

if (authorizationHeader != null) {

String createdBy = getCurrentUsername(authorizationHeader);


if (!event.getVisibility() || createdBy.equals(event.getCreatedBy())) {
return ResponseEntity.ok(event);
} else {

throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Access denied");
}
} else {

if (!event.getVisibility()) {
return ResponseEntity.ok(event);
} else {

throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Access denied");
}
}
} catch (ResponseStatusException ex) {
throw ex;
} catch (Exception ex) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", ex);
}
}




@GetMapping("/list")
@CrossOrigin(origins = "http://localhost:4200")
public ResponseEntity<List<Event>> listEvents() {
public ResponseEntity<List<Event>> listAllEvents(@RequestHeader(value = "Authorization", required = false) String authorizationHeader) {
try {
List<Event> events = eventRepository.findAll();
List<Event> events;
String createdBy = getCurrentUsername(authorizationHeader);

if (createdBy == null || "Usuario Desconocido".equals(createdBy)) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Event not found");
} else {
events = eventRepository.findByCreatedBy(createdBy);
}

return ResponseEntity.ok(events);
} catch (Exception ex) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", ex);
}
}

@GetMapping("/list/public")
public ResponseEntity<List<Event>> listPublicEvents() {
try {
List<Event> publicEvents = eventRepository.findByVisibilityFalse();
return ResponseEntity.ok(publicEvents);
} catch (Exception ex) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", ex);
}
}



@PostMapping("/create")
@CrossOrigin(origins = "http://localhost:4200")
public ResponseEntity<Event> createEvent(@RequestBody Event newEvent) {
public ResponseEntity<Event> createEvent(@RequestBody Event newEvent, @RequestHeader("Authorization") String authorizationHeader) {
try {
String createdBy = getCurrentUsername(authorizationHeader);
newEvent.setCreatedBy(createdBy);
Event createdEvent = eventRepository.save(newEvent);
return ResponseEntity.status(HttpStatus.CREATED).body(createdEvent);
} catch (Exception ex) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", ex);
}
}

public String getCurrentUsername(String authorizationHeader) {

String jwtToken = authorizationHeader.replace("Bearer ", "");


String username = getUsernameFromJWT(jwtToken);

return username;
}

private String getUsernameFromJWT(String jwtToken) {
try {
DecodedJWT decodedJWT = JWT.decode(jwtToken);
Claim preferredUsernameClaim = decodedJWT.getClaim("preferred_username");

if (!preferredUsernameClaim.isNull()) {
return preferredUsernameClaim.asString();
}
} catch (Exception e) {

}

return "Usuario Desconocido";
}

@PutMapping("/update/{id}")
@CrossOrigin(origins = "http://localhost:4200")
public ResponseEntity<String> updateEvent(@PathVariable Long id, @RequestBody Event updatedEvent) {
try {
Event event = eventRepository.findById(id)
Expand All @@ -83,6 +158,8 @@ public ResponseEntity<String> updateEvent(@PathVariable Long id, @RequestBody Ev
event.setType(updatedEvent.getType());
event.setUrl(updatedEvent.getUrl());
event.setState(updatedEvent.getState());
event.setDirection(updatedEvent.getDirection());
event.setVisibility(updatedEvent.getVisibility());

// Guarda el evento actualizado en el repositorio
eventRepository.save(event);
Expand All @@ -97,7 +174,6 @@ public ResponseEntity<String> updateEvent(@PathVariable Long id, @RequestBody Ev


@DeleteMapping("/delete/{id}")
@CrossOrigin(origins = "http://localhost:4200")
public ResponseEntity<Void> deleteEvent(@PathVariable Long id) {
try {
Event event = eventRepository.findById(id)
Expand All @@ -110,7 +186,6 @@ public ResponseEntity<Void> deleteEvent(@PathVariable Long id) {
}

@GetMapping("/city/{id}")
@CrossOrigin(origins = "http://localhost:4200")
public ResponseEntity<City> findCity(@PathVariable Long id) {
try {
City city = cityRepository.findById(id)
Expand All @@ -122,7 +197,6 @@ public ResponseEntity<City> findCity(@PathVariable Long id) {
}

@GetMapping("/listCity")
@CrossOrigin(origins = "http://localhost:4200")
public ResponseEntity<List<City>> listCities() {
try {
List<City> cities = cityRepository.findAll();
Expand All @@ -133,7 +207,6 @@ public ResponseEntity<List<City>> listCities() {
}

@GetMapping("/logistic/{id}")
@CrossOrigin(origins = "http://localhost:4200")
public ResponseEntity<Logistic> findLogistic(@PathVariable Long id) {
try {
Logistic logistic = logisticRepository.findById(id)
Expand All @@ -145,7 +218,6 @@ public ResponseEntity<Logistic> findLogistic(@PathVariable Long id) {
}

@GetMapping("/listLogistic")
@CrossOrigin(origins = "http://localhost:4200")
public ResponseEntity<List<Logistic>> listLogistics() {
try {
List<Logistic> logistics = logisticRepository.findAll();
Expand All @@ -159,4 +231,6 @@ public ResponseEntity<List<Logistic>> listLogistics() {
String index() {
return "index";
}


}
Loading