Commit cb57e13d authored by Ondřej Pavlica's avatar Ondřej Pavlica
Browse files

Merge branch 'feature/balga/rest' into 'main'

Rest controller

See merge request !39
parents 1c421be9 dfd60bbb
Pipeline #139527 passed with stages
in 2 minutes and 13 seconds
package cz.muni.fi.pa165.winery.webapp.rest.controllers;
import cz.muni.fi.pa165.winery.dto.wine.GrapeDto;
import cz.muni.fi.pa165.winery.services.wine.GrapeService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author Jakub Balga
*/
@RestController
@RequestMapping("/rest/grape")
public class GrapeRestController {
private final GrapeService grapeService;
public GrapeRestController(GrapeService grapeService) {
this.grapeService = grapeService;
}
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<GrapeDto> index() {
return grapeService.getAll();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public final GrapeDto getGrape(@PathVariable("id") long id) {
GrapeDto grape = grapeService.get(id);
if (grape == null) {
throw new ResourceNotFoundException();
}
return grape;
}
@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public final void createGrape(@RequestBody GrapeDto grape) {
if (grape == null) {
throw new NullPointerException("Grape DTO is null");
}
try {
grapeService.create(grape);
} catch (Exception ex) {
throw new IllegalArgumentException("Cannot create entity, already exists");
}
}
@RequestMapping(value = "/edit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public final void editGrape(@RequestBody GrapeDto grape) {
if (grape == null) {
throw new NullPointerException("Grape DTO is null");
}
try {
grapeService.update(grape);
} catch (Exception ex) {
throw new IllegalArgumentException("Entity with this ID does not exist, cannot update");
}
}
@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
public final void deleteGrape(@RequestBody GrapeDto grape) {
try {
grapeService.delete(grape);
} catch (Exception ex) {
throw new IllegalArgumentException("Entity with this ID does not exist, cannot delete");
}
}
}
package cz.muni.fi.pa165.winery.webapp.rest.controllers;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* @author Jakub Balga
*/
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason="The requested resource was not found")
public class ResourceNotFoundException extends RuntimeException { }
......@@ -38,7 +38,7 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/assets/**", "/css/**", "/js/**", "/error", "/login").permitAll()
.antMatchers("/assets/**", "/css/**", "/js/**", "/error", "/login", "/rest/**").permitAll()
.anyRequest().authenticated()
.and()
.csrf()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment