Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GC-66 Implement announcements #52

Merged
merged 2 commits into from
May 9, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.AllArgsConstructor;
import org.example.cmpe202_final.controller.course.strategy.CourseStrategy;
import org.example.cmpe202_final.model.announcement.Announcement;
import org.example.cmpe202_final.model.assignment.Assignment;
import org.example.cmpe202_final.model.course.Course;
import org.example.cmpe202_final.model.course.CourseInput;
import org.example.cmpe202_final.model.course.Semester;
import org.example.cmpe202_final.model.user.User;
import org.example.cmpe202_final.model.user.UserType;
import org.example.cmpe202_final.service.announcement.AnnouncementService;
import org.example.cmpe202_final.service.course.CourseService;
import org.example.cmpe202_final.service.semester.SemesterService;
import org.example.cmpe202_final.service.user.UserService;
Expand Down Expand Up @@ -35,6 +37,9 @@ public class CourseController {
@Autowired
private CourseService courseService;

@Autowired
private AnnouncementService announcementService;

@GetMapping
public ArrayList<CourseViewEntity> getCourses(@RequestParam(required = false) String userId) {
Optional<User> user = Optional.empty();
Expand Down Expand Up @@ -63,18 +68,19 @@ public List<User> getStudentsByCourseId(@PathVariable String courseId) {

@GetMapping("/{courseId}/details")
public CourseDetails getCourseDetails(@PathVariable String courseId) {
ArrayList<Announcement> announcements = announcementService.getAnnouncementsByCourse(courseId);
Optional<Course> course = courseService.findById(courseId);
if(course.isEmpty()){
return new CourseDetails(null, null);
return new CourseDetails(null, null, announcements);
}
if(course.get().getInstructor() == null){
return new CourseDetails(course.get(), null);
return new CourseDetails(course.get(), null, announcements);
}
Optional<User> instructor = userService.findById(course.get().getInstructor());
if(instructor.isEmpty()){
return new CourseDetails(course.get(), null);
return new CourseDetails(course.get(), null, announcements);
}
return new CourseDetails(course.get(), instructor.get());
return new CourseDetails(course.get(), instructor.get(), announcements);
}

@GetMapping("/input")
Expand All @@ -100,14 +106,32 @@ public ArrayList<CourseViewEntity> createCourse(@RequestBody Course course){

@PostMapping("/update")
public CourseDetails updateCourse(@RequestBody Course course) {
ArrayList<Announcement> announcements = announcementService.getAnnouncementsByCourse(course.getId());
Course updatedCourse = courseService.addItem(course);
if (updatedCourse.getInstructor() == null) {
return new CourseDetails(updatedCourse, null);
return new CourseDetails(updatedCourse, null, announcements);
}
Optional<User> instructor = userService.findById(updatedCourse.getInstructor());
if (instructor.isEmpty()) {
return new CourseDetails(updatedCourse, null);
return new CourseDetails(updatedCourse, null, announcements);
}
return new CourseDetails(updatedCourse, instructor.get());

return new CourseDetails(updatedCourse, instructor.get(), announcements);
}

@PostMapping("/announcement")
public CourseDetails postAnnouncement(@RequestBody Announcement announcement) {
announcementService.saveAnnouncement(announcement);
ArrayList<Announcement> announcements = announcementService.getAnnouncementsByCourse(announcement.getCourseId());
Course updatedCourse = courseService.findCourseById(announcement.getCourseId());
if (updatedCourse.getInstructor() == null) {
return new CourseDetails(updatedCourse, null, announcements);
}
Optional<User> instructor = userService.findById(updatedCourse.getInstructor());
if (instructor.isEmpty()) {
return new CourseDetails(updatedCourse, null, announcements);
}

return new CourseDetails(updatedCourse, instructor.get(), announcements);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.example.cmpe202_final.model.announcement;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "announcements")
public class Announcement {
@Id
private String id;
private String courseId;
private String title;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.example.cmpe202_final.repository.announcement;

import org.example.cmpe202_final.model.announcement.Announcement;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.ArrayList;
import java.util.Optional;

public interface AnnouncementRepository extends MongoRepository<Announcement, String> {
Optional<ArrayList<Announcement>> findByCourseId(String courseId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.example.cmpe202_final.service.announcement;

import lombok.AllArgsConstructor;
import org.example.cmpe202_final.model.announcement.Announcement;
import org.example.cmpe202_final.repository.announcement.AnnouncementRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Optional;

@AllArgsConstructor
@Service
public class AnnouncementService {
@Autowired
private AnnouncementRepository announcementRepository;

public ArrayList<Announcement> getAnnouncementsByCourse(String courseId){
Optional<ArrayList<Announcement>> optional = announcementRepository.findByCourseId(courseId);
return optional.orElseGet(ArrayList::new);
}

public Announcement saveAnnouncement(Announcement announcement){
return announcementRepository.save(announcement);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.example.cmpe202_final.model.announcement.Announcement;
import org.example.cmpe202_final.model.course.Course;
import org.example.cmpe202_final.model.user.User;

import java.util.ArrayList;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CourseDetails {
private Course course;
private User instructor;
private ArrayList<Announcement> announcements;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.example.cmpe202_final.controller.announcement;

import org.example.cmpe202_final.model.announcement.Announcement;
import org.example.cmpe202_final.repository.announcement.AnnouncementRepository;
import org.example.cmpe202_final.service.announcement.AnnouncementService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.ArrayList;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class AnnouncementServiceTest {

@Mock
private AnnouncementRepository announcementRepository;

@InjectMocks
private AnnouncementService announcementService;

@Test
public void testGetAnnouncementsByCourse() {
// Prepare test data
String courseId = "123";
ArrayList<Announcement> expectedAnnouncements = new ArrayList<>();
// Mock repository behavior
when(announcementRepository.findByCourseId(courseId)).thenReturn(Optional.of(expectedAnnouncements));

// Call service method
ArrayList<Announcement> result = announcementService.getAnnouncementsByCourse(courseId);

// Verify repository method is called
verify(announcementRepository, times(1)).findByCourseId(courseId);
// Verify result
assertEquals(expectedAnnouncements, result);
}

@Test
public void testSaveAnnouncement() {
// Prepare test data
Announcement announcementToSave = new Announcement(/* provide necessary constructor arguments */);
Announcement savedAnnouncement = new Announcement(/* provide necessary constructor arguments */);
// Mock repository behavior
when(announcementRepository.save(announcementToSave)).thenReturn(savedAnnouncement);

// Call service method
Announcement result = announcementService.saveAnnouncement(announcementToSave);

// Verify repository method is called
verify(announcementRepository, times(1)).save(announcementToSave);
// Verify result
assertEquals(savedAnnouncement, result);
}
}
Loading
Loading