Hangry/src/main/java/com/example/hangry/controller/GroupController.java
2025-02-28 21:25:24 +01:00

45 lines
1.7 KiB
Java

package com.example.hangry.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Set;
import com.example.hangry.*;
import com.example.hangry.services.GroupService;
@RestController
@RequestMapping("/api/groups")
@CrossOrigin(origins = "*")
public class GroupController {
private final GroupService groupService;
public GroupController(GroupService groupService) {
this.groupService = groupService;
}
// Neue Gruppe erstellen
@PostMapping
public ResponseEntity<Group> createGroup(@RequestParam String name, @RequestParam String description) {
return ResponseEntity.ok(groupService.createGroup(name, description));
}
// Gruppe beitreten
@PostMapping("/{groupId}/join/{userId}")
public ResponseEntity<String> joinGroup(@PathVariable Long groupId, @PathVariable Long userId) {
boolean joined = groupService.joinGroup(userId, groupId);
return joined ? ResponseEntity.ok("Gruppe beigetreten") : ResponseEntity.badRequest().body("Bereits Mitglied");
}
// Gruppe verlassen
@PostMapping("/{groupId}/leave/{userId}")
public ResponseEntity<String> leaveGroup(@PathVariable Long groupId, @PathVariable Long userId) {
boolean left = groupService.leaveGroup(userId, groupId);
return left ? ResponseEntity.ok("Gruppe verlassen") : ResponseEntity.badRequest().body("Nicht in der Gruppe");
}
// Mitglieder einer Gruppe abrufen
@GetMapping("/{groupId}/members")
public ResponseEntity<Set<User>> getGroupMembers(@PathVariable Long groupId) {
return ResponseEntity.ok(groupService.getGroupMembers(groupId));
}
}