Добавлены DTO для всех entity
This commit is contained in:
@@ -5,13 +5,14 @@ import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.serega6531.packmate.model.Packet;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.model.pojo.PacketDto;
|
||||
import ru.serega6531.packmate.service.StreamService;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/packet/")
|
||||
@@ -25,10 +26,12 @@ public class PacketController {
|
||||
}
|
||||
|
||||
@PostMapping("/{streamId}")
|
||||
public List<Packet> getPacketsForStream(@PathVariable long streamId) {
|
||||
public List<PacketDto> getPacketsForStream(@PathVariable long streamId) {
|
||||
final Optional<Stream> stream = streamService.find(streamId);
|
||||
if (stream.isPresent()) {
|
||||
return stream.get().getPackets();
|
||||
return stream.get().getPackets().stream()
|
||||
.map(streamService::packetToDto)
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ package ru.serega6531.packmate.controller;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.serega6531.packmate.model.Pattern;
|
||||
import ru.serega6531.packmate.model.pojo.PatternDto;
|
||||
import ru.serega6531.packmate.service.PatternService;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/pattern/")
|
||||
@@ -19,8 +21,10 @@ public class PatternController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Collection<Pattern> getPatterns() {
|
||||
return service.findAll();
|
||||
public List<PatternDto> getPatterns() {
|
||||
return service.findAll()
|
||||
.stream().map(service::toDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@PostMapping("/{id}")
|
||||
@@ -29,9 +33,11 @@ public class PatternController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Pattern addPattern(@RequestBody Pattern pattern) {
|
||||
pattern.setEnabled(true);
|
||||
return service.save(pattern);
|
||||
public PatternDto addPattern(@RequestBody PatternDto dto) {
|
||||
dto.setEnabled(true);
|
||||
Pattern pattern = service.fromDto(dto);
|
||||
Pattern saved = service.save(pattern);
|
||||
return service.toDto(saved);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ package ru.serega6531.packmate.controller;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.serega6531.packmate.model.CtfService;
|
||||
import ru.serega6531.packmate.model.pojo.ServiceDto;
|
||||
import ru.serega6531.packmate.service.ServicesService;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/service/")
|
||||
@@ -19,8 +21,10 @@ public class ServiceController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Collection<CtfService> getServices() {
|
||||
return service.findAll();
|
||||
public List<ServiceDto> getServices() {
|
||||
return service.findAll().stream()
|
||||
.map(service::toDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@DeleteMapping("/{port}")
|
||||
@@ -29,8 +33,9 @@ public class ServiceController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public CtfService addService(@RequestBody CtfService ctfService) {
|
||||
return service.save(ctfService);
|
||||
public CtfService addService(@RequestBody ServiceDto dto) {
|
||||
CtfService newService = this.service.fromDto(dto);
|
||||
return this.service.save(newService);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,42 +2,47 @@ package ru.serega6531.packmate.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.model.pojo.Pagination;
|
||||
import ru.serega6531.packmate.model.pojo.StreamDto;
|
||||
import ru.serega6531.packmate.service.StreamService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/stream/")
|
||||
public class StreamController {
|
||||
|
||||
private final StreamService streamService;
|
||||
private final StreamService service;
|
||||
|
||||
@Autowired
|
||||
public StreamController(StreamService streamService) {
|
||||
this.streamService = streamService;
|
||||
public StreamController(StreamService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@PostMapping("/all")
|
||||
public List<Stream> getStreams(@RequestBody Pagination pagination) {
|
||||
return streamService.findAll(pagination, Optional.empty(), pagination.isFavorites());
|
||||
public List<StreamDto> getStreams(@RequestBody Pagination pagination) {
|
||||
return service.findAll(pagination, Optional.empty(), pagination.isFavorites()).stream()
|
||||
.map(service::streamToDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@PostMapping("/{port}")
|
||||
public List<Stream> getStreams(@PathVariable int port, @RequestBody Pagination pagination) {
|
||||
return streamService.findAll(pagination, Optional.of(port), pagination.isFavorites());
|
||||
public List<StreamDto> getStreams(@PathVariable int port, @RequestBody Pagination pagination) {
|
||||
return service.findAll(pagination, Optional.of(port), pagination.isFavorites()).stream()
|
||||
.map(service::streamToDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/favorite")
|
||||
public void favoriteStream(@PathVariable long id) {
|
||||
streamService.setFavorite(id, true);
|
||||
service.setFavorite(id, true);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/unfavorite")
|
||||
public void unfavoriteStream(@PathVariable long id) {
|
||||
streamService.setFavorite(id, false);
|
||||
service.setFavorite(id, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user