Skip to content
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
8 changes: 0 additions & 8 deletions src/main/java/com/heftyb/dms/controllers/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -51,13 +50,6 @@ public String adminHome(HttpServletRequest request, Model model, Principal princ
return "hr_administrator";
}

@GetMapping({"/create_employee", "/create_employee/"})
public String create_employee_form(Principal principal, ModelMap map) {
map.addAttribute("username", principal.getName());

return "employee_form";
}

@PostMapping({"/create_employee", "/create_employee/"})
public String create_employee(@Valid Employee employee) {

Expand Down
26 changes: 7 additions & 19 deletions src/main/java/com/heftyb/dms/controllers/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,39 @@ public SearchController(final VehicleService vehicleService,
@GetMapping({"/", ""})
public String results(Principal principal,
ModelMap map,
@RequestParam(required = false) long custId,
@RequestParam(required = false) boolean isCool,
@RequestParam(required = false) String fname,
@RequestParam(required = false) String lname,
@RequestParam(required = false) String phone,
@RequestParam(required = false) String email,
@RequestParam(required = false) String vin,
@RequestParam(required = false) Long custId,
@RequestParam(required = false, defaultValue = "") String fname,
@RequestParam(required = false, defaultValue = "") String lname,
@RequestParam(required = false, defaultValue = "") String phone,
@RequestParam(required = false, defaultValue = "") String email,
@RequestParam(required = false, defaultValue = "") String vin,
@RequestParam(required = false) boolean custSearch,
@RequestParam(required = false) boolean vehicleSearch) {
map.addAttribute("username", principal.getName());

List<Customer> customers = new ArrayList<>();
List<Vehicle> vehicles = new ArrayList<>();

if (custId != 0) {
if (custId != null && custId != 0) {
Customer c = customerService.findById(custId);
if (custSearch) customers.add(c);
if (vehicleSearch) {
c.getVehicles().iterator().forEachRemaining(vehicles::add);
}
}

if (isCool) {
System.out.println("You're Cool!");
}

if (!fname.isEmpty() && !lname.isEmpty()) {
List<Customer> c = customerService.findByName(fname, lname);

if (custSearch) c.iterator().forEachRemaining(customers::add);
if (vehicleSearch) {
// c.stream().map(cust -> cust.getVehicles().iterator().forEachRemaining(vehicles::add));
final List<Vehicle> finalVehicles = vehicles;
c.stream().forEach(cust -> {
cust.getVehicles().iterator().forEachRemaining(finalVehicles::add);
});
finalVehicles.iterator().forEachRemaining(vehicles::add);
}
} else if (!lname.isEmpty()) {
// customerService.findByLastName(lname)
// .iterator().forEachRemaining(customers::add);
//
List<Customer> c = customerService.findByLastName(lname);

if (custSearch) c.iterator().forEachRemaining(customers::add);
Expand All @@ -82,18 +73,15 @@ public String results(Principal principal,
cust.getVehicles().iterator().forEachRemaining(finalVehicles::add);
});
vehicles = finalVehicles;
// finalVehicles.iterator().forEachRemaining(vehicles::add);
}
} else if (!fname.isEmpty()) {
// customerService.findByFirstName(fname).iterator().forEachRemaining(customers::add);
List<Customer> c = customerService.findByFirstName(fname);

if (custSearch) c.iterator().forEachRemaining(customers::add);
final List<Vehicle> finalVehicles = vehicles;
c.stream().forEach(cust -> {
cust.getVehicles().iterator().forEachRemaining(finalVehicles::add);
});
// finalVehicles.iterator().forEachRemaining(vehicles::add);
vehicles = finalVehicles;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.security.Principal;
Expand Down Expand Up @@ -38,11 +39,12 @@ public String vehicle_details(Principal principal, ModelMap map, @RequestParam l
return "vehicle_details";
}

@GetMapping({"/search", "/search/"})
// The shared vin-search fragment submits with POST, so this accepts both methods.
@RequestMapping(value = {"/search", "/search/"}, method = {RequestMethod.GET, RequestMethod.POST})
public String vin_search(Principal principal, ModelMap map, @RequestParam String vin) {
map.addAttribute("username", principal.getName());
if (vin.isBlank()) {
return "add_new_vehicle";
return "redirect:/vehicles";
}

List<Vehicle> vehicles = vehicleService.findByVin(vin);
Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ <h2 class="center-text">Search</h2>
<input type="number" name="custId" id="id" class="right-input" value="0" placeholder="60120">
<label for="id" class="input-label">Customer ID</label>
</div>
<div class="input-wrapper">
<input type="checkbox" name="isCool">
<label for="looking_cool" class="input-label-small">Cool Checkbox</label>
</div>
<div class="input-wrapper">
<input type="text" id="first_name" name="fname" class="right-input" placeholder="John">
<label for="first_name" class="input-label">First Name</label>
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/heftyb/dms/controllers/SearchControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.heftyb.dms.controllers;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@WithMockUser
class SearchControllerTest {

@Autowired
private MockMvc mvc;

@Test
void searchWithNoParametersRendersEmptyResults() throws Exception {
mvc.perform(get("/search"))
.andExpect(status().isOk())
.andExpect(view().name("results"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.heftyb.dms.vehicles.controllers;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@WithMockUser
class VehicleControllerTest {

@Autowired
private MockMvc mvc;

@Test
void vinSearchAcceptsThePostTheFormSends() throws Exception {
mvc.perform(post("/vehicles/search").with(csrf()).param("vin", "JH4TB2H26CC000000"))
.andExpect(status().isOk())
.andExpect(view().name("vehicles"));
}

@Test
void blankVinSearchRedirectsToTheVehicleList() throws Exception {
mvc.perform(get("/vehicles/search").param("vin", ""))
.andExpect(redirectedUrl("/vehicles"));
}
}
Loading