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

Receita #47

Merged
merged 7 commits into from
May 15, 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
@@ -1,16 +1,8 @@
package isep.ipp.pt.api.desofs.Config;

import isep.ipp.pt.api.desofs.DesofsApplication;
import isep.ipp.pt.api.desofs.Repository.Implementation.PacoteServiceImpl;
import isep.ipp.pt.api.desofs.Repository.Implementation.ReviewServiceImpl;
import isep.ipp.pt.api.desofs.Repository.Implementation.TipoPacoteServiceImpl;
import isep.ipp.pt.api.desofs.Repository.Implementation.UserRepoImpl;
import isep.ipp.pt.api.desofs.Repository.Implementation.EncomendaServiceImpl;
import isep.ipp.pt.api.desofs.Repository.Interface.EncomendaServiceRepo;
import isep.ipp.pt.api.desofs.Repository.Interface.PacoteServiceRepo;
import isep.ipp.pt.api.desofs.Repository.Interface.ReviewServiceRepo;
import isep.ipp.pt.api.desofs.Repository.Interface.TipoPacoteServiceRepo;
import isep.ipp.pt.api.desofs.Repository.Interface.UserServiceRepo;
import isep.ipp.pt.api.desofs.Repository.Implementation.*;
import isep.ipp.pt.api.desofs.Repository.Interface.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -59,6 +51,14 @@ public EncomendaServiceRepo mysqlEncomenda(){
public ReviewServiceRepo mysqlReview(){
return new ReviewServiceImpl();
}
@Bean
public TipoReceitaServiceRepo mysqlTipoReceita(){
return new TipoReceitaServiceImpl();
}
@Bean
public ReceitaServiceRepo mysqlReceita(){
return new ReceitaServiceImpl();
}

//Datasource
@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package isep.ipp.pt.api.desofs.Controllers;

import isep.ipp.pt.api.desofs.Dto.ReceitaDTO.ControllerLayer.ReceitaDTOPatchRequest;
import isep.ipp.pt.api.desofs.Dto.ReceitaDTO.ControllerLayer.ReceitaDTOResponse;
import isep.ipp.pt.api.desofs.Dto.ReceitaDTO.ControllerLayer.ReceitaDTOSaveRequest;
import isep.ipp.pt.api.desofs.Dto.ReceitaDTO.ServiceLayer.ReceitaDTOServicePatchRequest;
import isep.ipp.pt.api.desofs.Dto.ReceitaDTO.ServiceLayer.ReceitaDTOServiceRequest;
import isep.ipp.pt.api.desofs.Dto.ReceitaDTO.ServiceLayer.ReceitaDTOServiceResponse;
import isep.ipp.pt.api.desofs.Mapper.ReceitaMapper.ReceitaMapper;
import isep.ipp.pt.api.desofs.Service.ReceitaService.ReceitaService;
import isep.ipp.pt.api.desofs.Utils.PersonalValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/receita")
public class ReceitaController {
@Autowired
private ReceitaService receitaService;
@Autowired
private ReceitaMapper receitaMapper;
@Autowired
private PersonalValidation validation;


@PostMapping("/save")
public ResponseEntity<ReceitaDTOResponse> saveReceita(@RequestBody ReceitaDTOSaveRequest receita) {
if (!validation.validate(receita)) {
return ResponseEntity.badRequest().build();
}

try {
ReceitaDTOServiceRequest receitaRequestService = receitaMapper.toReceitaDtoServiceRequestFromReceitaDtoSaveRequest(receita);
if (!validation.validate(receitaRequestService)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
ReceitaDTOServiceResponse receitaServiceResponse = receitaService.save(receitaRequestService);
return ResponseEntity.ok( receitaMapper.fromReceitaToDto(receitaServiceResponse));

} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/get/{receitaId}")
public ResponseEntity<ReceitaDTOResponse> getReceita(@PathVariable Long receitaId) {
if (receitaId < 0) return ResponseEntity.badRequest().build();
try {

ReceitaDTOServiceResponse receitaServiceResponse = receitaService.findbyId(receitaId);
return ResponseEntity.ok( receitaMapper.fromReceitaToDto(receitaServiceResponse));

} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@PatchMapping("/update")
public ResponseEntity<ReceitaDTOResponse> updateReceita(@RequestBody ReceitaDTOPatchRequest receita) {
if (!validation.validate(receita)) {
return ResponseEntity.badRequest().build();
}
try {
ReceitaDTOServicePatchRequest receitaRequestService = receitaMapper.toReceitaDTOServicePAtchRequestFromReceitaDTOPatchRequest(receita);
if (!validation.validate(receitaRequestService)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
ReceitaDTOServiceResponse receitaServiceResponse = receitaService.update(receitaRequestService);
return ResponseEntity.ok( receitaMapper.fromReceitaToDto(receitaServiceResponse));
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@DeleteMapping("/delete/{receitaId}")
public ResponseEntity deleteReceita(@PathVariable Long receitaId) {
if (receitaId < 0) return ResponseEntity.badRequest().build();
receitaService.deleteById(receitaId);
return ResponseEntity.ok().build();
}

@GetMapping("/all")
public ResponseEntity<List<ReceitaDTOResponse>> getAllReceitas() {
try {
List<ReceitaDTOResponse> receitas = receitaMapper.fromReceitaDtoServiceResponseListToReceitaDToResponseList(receitaService.findAll());
return ResponseEntity.ok(receitas);
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package isep.ipp.pt.api.desofs.Controllers;

import isep.ipp.pt.api.desofs.Dto.TipoReceitaDTO.ControllerLayer.TipoReceitaDTOResponse;
import isep.ipp.pt.api.desofs.Dto.TipoReceitaDTO.ControllerLayer.TipoReceitaDTOSaveRequest;
import isep.ipp.pt.api.desofs.Dto.TipoReceitaDTO.ServiceLayer.TipoReceitaDTOServiceRequest;
import isep.ipp.pt.api.desofs.Dto.TipoReceitaDTO.ServiceLayer.TipoReceitaDTOServiceResponse;
import isep.ipp.pt.api.desofs.Mapper.TipoReceitaMapper.TipoReceitaMapper;
import isep.ipp.pt.api.desofs.Service.TipoReceitaService.TipoReceitaService;
import isep.ipp.pt.api.desofs.Utils.PersonalValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tipoReceita")
public class TipoReceitaController {

@Autowired
private TipoReceitaService tipoReceitaService;
@Autowired
private TipoReceitaMapper tipoReceitaMapper;
@Autowired
private PersonalValidation validation;

@PostMapping("/save")
public ResponseEntity<TipoReceitaDTOResponse> save(@RequestBody TipoReceitaDTOSaveRequest tipoReceitaRequest) {
if (!validation.validate(tipoReceitaRequest)) {
return ResponseEntity.badRequest().build();
}
try {
TipoReceitaDTOServiceRequest tipoReceitaServiceRequest = tipoReceitaMapper.toTipoReceitaDTOServiceRequestFromTipoReceitaDTOSaveRequest(tipoReceitaRequest);
if (!validation.validate(tipoReceitaServiceRequest)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

TipoReceitaDTOResponse tipoReceitaDTOResponse = tipoReceitaMapper.toTipoReceitaDTOResponseFromTipoReceitaDTOServiceResponse(tipoReceitaService.save(tipoReceitaServiceRequest));
return ResponseEntity.ok(tipoReceitaDTOResponse);
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/{tipoReceitaId}")
public ResponseEntity<TipoReceitaDTOResponse> getTipoReceita(@PathVariable Long tipoReceitaId) {
if (tipoReceitaId < 0) return ResponseEntity.badRequest().build();
try {
TipoReceitaDTOServiceResponse tipoReceitaServiceResponse = tipoReceitaService.findbyId(tipoReceitaId);
TipoReceitaDTOResponse tipoReceitaDTOResponse = tipoReceitaMapper.toTipoReceitaDTOResponseFromTipoReceitaDTOServiceResponse(tipoReceitaServiceResponse);
return ResponseEntity.ok(tipoReceitaDTOResponse);
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/list")
public ResponseEntity<List<TipoReceitaDTOResponse>> getTipoReceitaList() {
try {
List<TipoReceitaDTOServiceResponse> tipoReceitaServiceResponseList = tipoReceitaService.findAll();
List<TipoReceitaDTOResponse> tipoReceitaDTOResponseList = tipoReceitaMapper.toTipoReceitaDTOResponseListFromTipoReceitaDTOServiceResponseList(tipoReceitaServiceResponseList);
return ResponseEntity.ok(tipoReceitaDTOResponseList);
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@DeleteMapping("/delete/All")
public ResponseEntity deleteAll() {
tipoReceitaService.deleteAll();
return ResponseEntity.ok().build();
}

@DeleteMapping("/delete/{tipoReceitaId}")
public ResponseEntity deleteById(@PathVariable Long tipoReceitaId) {
if (tipoReceitaId < 0) return ResponseEntity.badRequest().build();
tipoReceitaService.deleteById(tipoReceitaId);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package isep.ipp.pt.api.desofs.Dto.ReceitaDTO.ControllerLayer;

import isep.ipp.pt.api.desofs.Model.Pacote;
import isep.ipp.pt.api.desofs.Model.Receita;
import isep.ipp.pt.api.desofs.Model.TipoReceita;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;

public class ReceitaDTOPatchRequest {
@Id
@GeneratedValue
private Long receitaId;
@Pattern(regexp = "^(\\.\\/[\\w-]+(\\/[\\w-]+)*\\/[\\w-]+\\.[\\w-]+)$", message = "Invalid path")
private String path;
@Pattern(regexp = "^[a-zA-Z0-9]*$", message = "Invalid name")
@NotNull
private String nome;
@ManyToOne
private Long pacote;

@ManyToOne
@NotNull
private Long tipoReceita;

public ReceitaDTOPatchRequest() {
}

public ReceitaDTOPatchRequest(Long receitaId, String path, String nome, Long pacote, Long tipoReceita) {
this.receitaId = receitaId;
this.path = path;
this.nome = nome;
this.pacote = pacote;
this.tipoReceita = tipoReceita;
}

public ReceitaDTOPatchRequest(String path, String nome, Long pacote, Long tipoReceita) {
this.path = path;
this.nome = nome;
this.pacote = pacote;
this.tipoReceita = tipoReceita;
}

public Long getReceitaId() {
return receitaId;
}

public String getPath() {
return path;
}


public String getNome() {
return nome;
}


public Long getPacote() {
return pacote;
}

public Long getTipoReceita() {
return tipoReceita;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package isep.ipp.pt.api.desofs.Dto.ReceitaDTO.ControllerLayer;

import isep.ipp.pt.api.desofs.Model.Pacote;
import isep.ipp.pt.api.desofs.Model.TipoReceita;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;

public class ReceitaDTOResponse {
@Id
@GeneratedValue
private Long receitaId;
@Pattern(regexp = "^(\\.\\/[\\w-]+(\\/[\\w-]+)*\\/[\\w-]+\\.[\\w-]+)$", message = "Invalid path")
private String path;
@Pattern(regexp = "^[a-zA-Z0-9]*$", message = "Invalid name")
@NotNull
private String nome;
@ManyToOne
private Pacote pacote;

@ManyToOne
@NotNull
private TipoReceita tipoReceita;

public ReceitaDTOResponse() {
}

public ReceitaDTOResponse(Long receitaId, String path, String nome, Pacote pacote, TipoReceita tipoReceita) {
this.receitaId = receitaId;
this.path = path;
this.nome = nome;
this.pacote = pacote;
this.tipoReceita = tipoReceita;
}

public ReceitaDTOResponse(String path, String nome, Pacote pacote, TipoReceita tipoReceita) {
this.path = path;
this.nome = nome;
this.pacote = pacote;
this.tipoReceita = tipoReceita;
}

public Long getReceitaId() {
return receitaId;
}

public String getPath() {
return path;
}

public String getNome() {
return nome;
}

public Pacote getPacote() {
return pacote;
}

public TipoReceita getTipoReceita() {
return tipoReceita;
}
}
Loading
Loading