Skip to content

Commit

Permalink
Merge pull request #49 from ucsb-cs156-s24/Ruizhe-DeleteButton
Browse files Browse the repository at this point in the history
Ruizhe - Add backend part for delete buttum feature
  • Loading branch information
pconrad authored May 29, 2024
2 parents 6cb87af + 46d346e commit ad4322d
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# proj-courses

This repo contains the code for the CMPSC 156 legacy code project "Courses Search".

The project provides a web application where users can search for UCSB courses in various ways.

Users with a Google Account can also store past, current or future schedules of courses for particular quarters.

# Deployments

| Type | Link |
|------|------------|
|------|------------|
| prod | <https://courses.dokku-00.cs.ucsb.edu/> |
| qa | <https://courses-qa.dokku-00.cs.ucsb.edu/> |

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.ucsb.cs156.courses.controllers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.ucsb.cs156.courses.documents.Course;
import edu.ucsb.cs156.courses.entities.PSCourse;
Expand All @@ -14,9 +15,11 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.ArrayList;
import java.util.Iterator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -59,4 +62,47 @@ public ArrayList<Course> getSectionsByPsId(@Parameter(name = "psId") @RequestPar
}
return sections;
}

@Operation(summary = "Delete a course with related lectures and sections")
@PreAuthorize("hasRole('ROLE_USER')")
@DeleteMapping("/delete")
public Object deleteSchedule(
@Parameter(name = "psId") @RequestParam Long psId,
@Parameter(name = "enrollCd") @RequestParam String enrollCd)
throws JsonProcessingException {
User currentUser = getCurrentUser().getUser();
PersonalSchedule ps =
personalScheduleRepository
.findByIdAndUser(psId, currentUser)
.orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, psId));

Iterable<PSCourse> courses = coursesRepository.findAllByPsId(psId);
ArrayList<String> relatedEnrollCodes = new ArrayList<>();
String body = ucsbCurriculumService.getAllSections(enrollCd, ps.getQuarter());

if (!body.equals("{\"error\": \"401: Unauthorized\"}")
&& !body.equals("{\"error\": \"Enroll code doesn't exist in that quarter.\"}")) {
Iterator<JsonNode> it = objectMapper.readTree(body).path("classSections").elements();
while (it.hasNext()) {
JsonNode classSection = it.next();
String sectionEnrollCd = classSection.path("enrollCode").asText();
relatedEnrollCodes.add(sectionEnrollCd);
}
}

boolean courseExist = false;
for (PSCourse course : courses) {
if (relatedEnrollCodes.contains(course.getEnrollCd())) {
courseExist = true;
coursesRepository.delete(course);
}
}
if (!courseExist) {
throw new EntityNotFoundException(PSCourse.class, "psId: " + psId + " enrollCd: " + enrollCd);
}

return genericMessage(
"Personal Schedule with psId %s and lectures and affiliated sections with enrollCd %s deleted"
.formatted(psId, enrollCd));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

Expand Down Expand Up @@ -109,4 +110,161 @@ public void api_psid_sections__user_logged_in__returns_existing_course() throws
String responseString = response.getResponse().getContentAsString();
assertEquals(expectedJson, responseString);
}

// test for /api/personalSections/delete
@WithMockUser(roles = {"USER"})
@Test
public void api_deleteSchedule_user_logged_in_can_successfully_delete() throws Exception {
User u = currentUserService.getCurrentUser().getUser();
PersonalSchedule ps =
PersonalSchedule.builder()
.name("Test")
.description("Test Description")
.quarter("20221")
.user(u)
.id(1L)
.build();
PSCourse course = PSCourse.builder().id(1L).user(u).enrollCd("80300").psId(1L).build();
ArrayList<PSCourse> crs = new ArrayList<>();
crs.add(course);
when(personalscheduleRepository.findByIdAndUser(eq(1L), eq(u))).thenReturn(Optional.of(ps));
when(coursesRepository.findAllByPsId(eq(1L))).thenReturn(crs);
when(ucsbCurriculumService.getAllSections(eq("80300"), eq("20221")))
.thenReturn("{\"classSections\": [{\"enrollCode\": \"80300\"}]}");
MvcResult response =
mockMvc
.perform(
delete("/api/personalSections/delete")
.param("psId", "1")
.param("enrollCd", "80300")
.with(csrf()))
.andExpect(status().isOk())
.andReturn();
verify(coursesRepository, times(1)).delete(course);
String responseString = response.getResponse().getContentAsString();
assertEquals(
"{\"message\":\"Personal Schedule with psId 1 and lectures and affiliated sections with enrollCd 80300 deleted\"}",
responseString);
}

// test for delete branch no schedule
@WithMockUser(roles = {"USER"})
@Test
public void api_deleteSchedule_user_logged_in_cannot_find_schedule() throws Exception {
User u = currentUserService.getCurrentUser().getUser();
when(personalscheduleRepository.findByIdAndUser(eq(1L), eq(u))).thenReturn(Optional.empty());
MvcResult response =
mockMvc
.perform(
delete("/api/personalSections/delete")
.param("psId", "1")
.param("enrollCd", "80300")
.with(csrf()))
.andExpect(status().isNotFound())
.andReturn();
String responseString = response.getResponse().getContentAsString();
boolean correct = responseString.contains("EntityNotFoundException");
assertEquals(true, correct);
}

// test for delete branch no course
@WithMockUser(roles = {"USER"})
@Test
public void api_deleteSchedule_user_logged_in_cannot_find_course() throws Exception {
User u = currentUserService.getCurrentUser().getUser();
PersonalSchedule ps =
PersonalSchedule.builder()
.name("Test")
.description("Test Description")
.quarter("20221")
.user(u)
.id(1L)
.build();
ArrayList<PSCourse> crs = new ArrayList<>();
String body = PersonalSectionsFixtures.ONE_COURSE;
when(personalscheduleRepository.findByIdAndUser(eq(1L), eq(u))).thenReturn(Optional.of(ps));
when(coursesRepository.findAllByPsId(eq(1L))).thenReturn(crs);
when(ucsbCurriculumService.getAllSections(eq("80300"), eq("20221"))).thenReturn(body);
MvcResult response =
mockMvc
.perform(
delete("/api/personalSections/delete")
.param("psId", "1")
.param("enrollCd", "80300")
.with(csrf()))
.andExpect(status().isNotFound())
.andReturn();
String responseString = response.getResponse().getContentAsString();
boolean correct = responseString.contains("EntityNotFoundException");
assertEquals(true, correct);
}

// test for delete branch no enroll code
@WithMockUser(roles = {"USER"})
@Test
public void api_deleteSchedule_user_logged_in_cannot_find_enrollCd() throws Exception {
User u = currentUserService.getCurrentUser().getUser();
PersonalSchedule ps =
PersonalSchedule.builder()
.name("Test")
.description("Test Description")
.quarter("20221")
.user(u)
.id(1L)
.build();
PSCourse course = PSCourse.builder().id(1L).user(u).enrollCd("80300").psId(1L).build();
ArrayList<PSCourse> crs = new ArrayList<>();
crs.add(course);
when(personalscheduleRepository.findByIdAndUser(eq(1L), eq(u))).thenReturn(Optional.of(ps));
when(coursesRepository.findAllByPsId(eq(1L))).thenReturn(crs);
when(ucsbCurriculumService.getAllSections(eq("80300"), eq("20221")))
.thenReturn("{\"error\": \"Enroll code doesn't exist in that quarter.\"}");
MvcResult response =
mockMvc
.perform(
delete("/api/personalSections/delete")
.param("psId", "1")
.param("enrollCd", "80300")
.with(csrf()))
.andExpect(status().isNotFound())
.andReturn();
verify(coursesRepository, times(0)).delete(course);
String responseString = response.getResponse().getContentAsString();
boolean correct = responseString.contains("EntityNotFoundException");
assertEquals(true, correct);
}

// test for branch 401 unauthorized
@WithMockUser(roles = {"USER"})
@Test
public void api_deleteSchedule_user_logged_in_unauthorized() throws Exception {
User u = currentUserService.getCurrentUser().getUser();
PersonalSchedule ps =
PersonalSchedule.builder()
.name("Test")
.description("Test Description")
.quarter("20221")
.user(u)
.id(1L)
.build();
PSCourse course = PSCourse.builder().id(1L).user(u).enrollCd("80300").psId(1L).build();
ArrayList<PSCourse> crs = new ArrayList<>();
crs.add(course);
when(personalscheduleRepository.findByIdAndUser(eq(1L), eq(u))).thenReturn(Optional.of(ps));
when(coursesRepository.findAllByPsId(eq(1L))).thenReturn(crs);
when(ucsbCurriculumService.getAllSections(eq("80300"), eq("20221")))
.thenReturn("{\"error\": \"401: Unauthorized\"}");
MvcResult response =
mockMvc
.perform(
delete("/api/personalSections/delete")
.param("psId", "1")
.param("enrollCd", "80300")
.with(csrf()))
.andExpect(status().isNotFound())
.andReturn();
String responseString = response.getResponse().getContentAsString();
boolean correct = responseString.contains("EntityNotFoundException");
assertEquals(true, correct);
}
}

0 comments on commit ad4322d

Please sign in to comment.