不知道有没有问题先提交一版
This commit is contained in:
parent
fd0cb2d345
commit
14dcc9de06
@ -0,0 +1,77 @@
|
||||
package org.cmh.backend.MeetingManagement.controller;
|
||||
|
||||
import org.cmh.backend.MeetingManagement.model.Meeting;
|
||||
import org.cmh.backend.MeetingManagement.service.MeetingService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/meetings")
|
||||
public class MeetingController {
|
||||
|
||||
@Autowired
|
||||
private MeetingService meetingService;
|
||||
|
||||
@GetMapping("/listAll")
|
||||
public ResponseEntity<List<Meeting>> listAll() {
|
||||
List<Meeting> data = meetingService.getAllMeetings();
|
||||
return ResponseEntity.ok(data);
|
||||
}
|
||||
|
||||
@PostMapping("/addMeeting")
|
||||
public ResponseEntity<Meeting> add(@RequestBody Map<String, String> credentials) {
|
||||
try {
|
||||
Meeting meeting = new Meeting();
|
||||
if (credentials.get("id") != null) {
|
||||
meeting.setId(Long.parseLong(credentials.get("id")));
|
||||
}
|
||||
meeting.setName(credentials.get("name"));
|
||||
meeting.setOrganizer(credentials.get("organizer"));
|
||||
meeting.setStartTime(LocalDateTime.parse(credentials.get("startTime")));
|
||||
meeting.setEndTime(LocalDateTime.parse(credentials.get("endTime")));
|
||||
meeting.setContent(credentials.get("content"));
|
||||
meeting.setStatus(credentials.get("status"));
|
||||
|
||||
Meeting meetingAdd = meetingService.createMeeting(meeting);
|
||||
return new ResponseEntity<>(meetingAdd, HttpStatus.CREATED);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/deleteMeeting")
|
||||
public ResponseEntity<String> delete(@RequestBody Map<String, String> credentials) {
|
||||
try {
|
||||
meetingService.deleteMeeting(Long.valueOf(credentials.get("id")));
|
||||
return new ResponseEntity<>("success", HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>("failure", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/updateMeeting")
|
||||
public ResponseEntity<Meeting> update(@RequestBody Meeting meeting) {
|
||||
try {
|
||||
Meeting updatedMeeting = meetingService.updateMeeting(meeting.getId(), meeting);
|
||||
return new ResponseEntity<>(updatedMeeting, HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/getMeetingById")
|
||||
public ResponseEntity<Meeting> getById(@RequestBody Map<String, String> credentials) {
|
||||
try {
|
||||
Meeting meeting = meetingService.getMeetingById(Long.valueOf(credentials.get("id"))).orElse(null);
|
||||
return new ResponseEntity<>(meeting, HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package org.cmh.backend.MeetingManagement.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@Entity
|
||||
public class Meeting {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
private String organizer;
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
private String content;
|
||||
private String status;
|
||||
|
||||
public Meeting() {}
|
||||
|
||||
public Meeting(String name, String organizer, LocalDateTime startTime, LocalDateTime endTime, String content, String status) {
|
||||
this.name = name;
|
||||
this.organizer = organizer;
|
||||
this.startTime = startTime;
|
||||
this.endTime = endTime;
|
||||
this.content = content;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Meeting{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", organizer='" + organizer + '\'' +
|
||||
", startTime=" + startTime +
|
||||
", endTime=" + endTime +
|
||||
", content='" + content + '\'' +
|
||||
", status='" + status + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package org.cmh.backend.MeetingManagement.repository;
|
||||
|
||||
import org.cmh.backend.MeetingManagement.model.Meeting;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface MeetingRepository extends JpaRepository<Meeting, Long> {
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package org.cmh.backend.MeetingManagement.service;
|
||||
|
||||
import org.cmh.backend.MeetingManagement.model.Meeting;
|
||||
import org.cmh.backend.MeetingManagement.repository.MeetingRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class MeetingService {
|
||||
@Autowired
|
||||
private MeetingRepository meetingRepository;
|
||||
|
||||
public List<Meeting> getAllMeetings() {
|
||||
return meetingRepository.findAll();
|
||||
}
|
||||
|
||||
public Optional<Meeting> getMeetingById(Long id) {
|
||||
return meetingRepository.findById(id);
|
||||
}
|
||||
|
||||
public Meeting createMeeting(Meeting meeting) {
|
||||
validateMeeting(meeting);
|
||||
return meetingRepository.save(meeting);
|
||||
}
|
||||
|
||||
public Meeting updateMeeting(Long id, Meeting meetingDetails) {
|
||||
validateMeeting(meetingDetails);
|
||||
Meeting meeting = meetingRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("Meeting not found with id " + id));
|
||||
meeting.setName(meetingDetails.getName());
|
||||
meeting.setOrganizer(meetingDetails.getOrganizer());
|
||||
meeting.setStartTime(meetingDetails.getStartTime());
|
||||
meeting.setEndTime(meetingDetails.getEndTime());
|
||||
meeting.setContent(meetingDetails.getContent());
|
||||
meeting.setStatus(meetingDetails.getStatus());
|
||||
return meetingRepository.save(meeting);
|
||||
}
|
||||
|
||||
public void deleteMeeting(Long id) {
|
||||
Meeting meeting = meetingRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("Meeting not found with id " + id));
|
||||
meetingRepository.delete(meeting);
|
||||
}
|
||||
|
||||
private void validateMeeting(Meeting meeting) {
|
||||
if (meeting.getName() == null || meeting.getName().isEmpty()) {
|
||||
throw new RuntimeException("会议名称不能为空");
|
||||
}
|
||||
if (meeting.getOrganizer() == null || meeting.getOrganizer().isEmpty()) {
|
||||
throw new RuntimeException("组织者不能为空");
|
||||
}
|
||||
if (meeting.getStartTime() == null) {
|
||||
throw new RuntimeException("开始时间不能为空");
|
||||
}
|
||||
if (meeting.getEndTime() == null) {
|
||||
throw new RuntimeException("结束时间不能为空");
|
||||
}
|
||||
if (meeting.getStatus() == null || meeting.getStatus().isEmpty()) {
|
||||
throw new RuntimeException("状态不能为空");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user