Skip to content

Commit 2a9ee29

Browse files
authored
Merge pull request #7 from ls1intum/orgUpdate
update org
2 parents c7efbf2 + 2186ae4 commit 2a9ee29

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/main/java/com/ase/angelos_kb_backend/controller/OrganisationController.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.ase.angelos_kb_backend.util.JwtUtil;
66

77
import java.util.List;
8+
import java.util.UUID;
89
import java.util.stream.Collectors;
910

1011
import org.springframework.http.HttpStatus;
@@ -52,6 +53,24 @@ public ResponseEntity<OrganisationDTO> addOrganisation(
5253
return ResponseEntity.status(HttpStatus.CREATED).body(newOrganisation);
5354
}
5455

56+
/**
57+
* Update an existing organisation. Only accessible by system admin.
58+
*/
59+
@PutMapping("/{id}")
60+
public ResponseEntity<OrganisationDTO> updateOrganisation(
61+
@RequestHeader("Authorization") String token,
62+
@PathVariable Long id,
63+
@RequestBody OrganisationDTO organisation) {
64+
if (!jwtUtil.extractIsSystemAdmin(token.replace("Bearer ", ""))) {
65+
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
66+
}
67+
OrganisationDTO newOrganisation = organisationService.updateOrganisation(id, organisation);
68+
if (newOrganisation == null) {
69+
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
70+
}
71+
return ResponseEntity.ok(newOrganisation);
72+
}
73+
5574
/**
5675
* Remove an organisation by ID. Only accessible by system admin.
5776
*/

src/main/java/com/ase/angelos_kb_backend/service/OrganisationService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.ase.angelos_kb_backend.service;
22

33
import java.util.List;
4+
import java.util.Optional;
45
import java.util.stream.Collectors;
56

67
import org.springframework.stereotype.Service;
@@ -33,6 +34,18 @@ public OrganisationDTO addOrganisation(String name) {
3334
Organisation savedOrganisation = organisationRepository.save(organisation);
3435
return convertToDto(savedOrganisation);
3536
}
37+
public OrganisationDTO updateOrganisation(Long id, OrganisationDTO updatedOrganisation) {
38+
Optional<Organisation> optionalOrg = organisationRepository.findById(id);
39+
if(optionalOrg.isEmpty()) {
40+
return null;
41+
}
42+
Organisation existingOrg = optionalOrg.get();
43+
if (updatedOrganisation.getName() != null) {
44+
existingOrg.setName(updatedOrganisation.getName());
45+
}
46+
Organisation savedOrg = organisationRepository.save(existingOrg);
47+
return convertToDto(savedOrg);
48+
}
3649

3750
public boolean removeOrganisation(Long orgId) {
3851
if (organisationRepository.existsById(orgId)) {

0 commit comments

Comments
 (0)