Commit d8d81942 authored by Marek Skácelík's avatar Marek Skácelík 🫠
Browse files

Merge branch 'deleteManufacturer' into 'milestone-2'

Deleted manufacturer

See merge request !38
parents dd08cb18 85c82302
Loading
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
package cz.muni.fi.pa165.core.company;

import cz.muni.fi.pa165.core.common.DomainObject;
import cz.muni.fi.pa165.core.device.Device;
import cz.muni.fi.pa165.core.house.House;
import cz.muni.fi.pa165.core.user.roles.CompanyRole;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
@@ -23,4 +25,11 @@ public class Company extends DomainObject {
	private String name;
	@OneToMany(mappedBy = "company")
	private List<CompanyRole> employeeList;

	@OneToMany(mappedBy = "company")
	private List<Device> deviceList;

	@OneToMany
	@JoinColumn(name = "domain_company_id")
	private List<House> houseList;
}
+2 −2
Original line number Diff line number Diff line
package cz.muni.fi.pa165.core.device;

import cz.muni.fi.pa165.core.common.DomainObject;
import cz.muni.fi.pa165.core.manufacturer.Manufacturer;
import cz.muni.fi.pa165.core.company.Company;
import cz.muni.fi.pa165.core.smartmeter.SmartMeter;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
@@ -28,5 +28,5 @@ public class Device extends DomainObject {
  private List<SmartMeter> smartMeterList;

  @ManyToOne
  private Manufacturer manufacturer;
  private Company company;
}
+15 −1
Original line number Diff line number Diff line
package cz.muni.fi.pa165.core.device;

import cz.muni.fi.pa165.core.common.DomainFacade;
import cz.muni.fi.pa165.core.company.CompanyService;
import cz.muni.fi.pa165.core.helpers.exceptions.EntityDeletionException;
import cz.muni.fi.pa165.core.smartmeter.SmartMeter;
import cz.muni.fi.pa165.model.dto.device.DeviceCreateDto;
import cz.muni.fi.pa165.model.dto.device.DeviceDto;
import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterCreateDto;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterDto;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -14,10 +18,20 @@ public class DeviceFacade extends DomainFacade<Device, DeviceDto, DeviceCreateDt

	private final DeviceService deviceService; // For the "DeviceService" specific methods

	private final CompanyService companyService;

	@Autowired
	public DeviceFacade(DeviceService deviceService, DeviceMapper deviceMapper) {
	public DeviceFacade(DeviceService deviceService, DeviceMapper deviceMapper, CompanyService companyService) {
		super(deviceService, deviceMapper);
		this.deviceService = deviceService;
		this.companyService = companyService;
	}

	@Override
	public DeviceDto create(DeviceCreateDto deviceCreateDto){
		Device device = mapper.fromCreateDto(deviceCreateDto);
		device.setCompany(companyService.findById(deviceCreateDto.getCompanyId()));
		return mapper.toDto(deviceService.create(device));
	}

	@SneakyThrows
+0 −31
Original line number Diff line number Diff line
package cz.muni.fi.pa165.core.manufacturer;

import cz.muni.fi.pa165.core.common.DomainObject;
import cz.muni.fi.pa165.core.device.Device;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "domain_manufacturer")
public class Manufacturer extends DomainObject {

  @Column(unique = true, nullable = false)
  private String name;

  @OneToMany(mappedBy = "manufacturer")
  private List<Device> deviceList;
}
+0 −123
Original line number Diff line number Diff line
package cz.muni.fi.pa165.core.manufacturer;

import cz.muni.fi.pa165.model.dto.manufacturer.ManufacturerCreateDto;
import cz.muni.fi.pa165.model.dto.manufacturer.ManufacturerDto;
import cz.muni.fi.pa165.model.dto.manufacturer.ManufacturerUpdateDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Controller for managing manufacturers.
 */
@RestController
@RequestMapping("/api/manufacturer")
@Tag(name = "manufacturer", description = "Manufacturer API")
public class ManufacturerController {
	private final ManufacturerFacade manufacturerFacade;

	@Autowired
	public ManufacturerController(
			ManufacturerFacade manufacturerFacade
	) {
		this.manufacturerFacade = manufacturerFacade;
	}

	@Operation(
			summary = "Get all manufacturers (paginated)",
			description = "Returns a pageable list of all manufacturers.",
			tags= {"manufacturer"})
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "Successfully retrieved list of manufacturers"),
			@ApiResponse(responseCode = "400", description = "Invalid pagination parameters"),
			@ApiResponse(responseCode = "404", description = "No manufacturers found")
	})
	@GetMapping("pageable")
	public List<ManufacturerDto> findAllPageable(Pageable pageable) {
		return manufacturerFacade.findAllPageable(pageable);
	}

	@Operation(
			summary = "Get all manufacturers",
			description = "Returns a list of all manufacturers.",
			tags= {"manufacturer"})
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "Successfully retrieved list of manufacturers"),
			@ApiResponse(responseCode = "404", description = "No manufacturers found")
	})
	@CrossOrigin(origins = "*")
	@GetMapping // CORS headers needed for JavaScript clients
	public List<ManufacturerDto> findAll() {
		return manufacturerFacade.findAll();
	}

	@Operation(
			summary = "Get manufacturer by ID",
			description = "Returns the manufacturer with the specified ID.",
			tags= {"manufacturer"})
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "Successfully retrieved manufacturer"),
			@ApiResponse(responseCode = "404", description = "Manufacturer not found")
	})
	@GetMapping("/{id}")
	public ManufacturerDto findById(
			@PathVariable @Parameter(description = "The ID of the manufacturer.") String id) {
		return manufacturerFacade.findById(id);
	}

	@Operation(
			summary = "Create manufacturer",
			description = "Creates a new manufacturer.",
			tags= {"manufacturer"})
	@ApiResponses(value = {
			@ApiResponse(responseCode = "201", description = "Manufacturer created successfully"),
			@ApiResponse(responseCode = "400", description = "Invalid manufacturer data supplied")
	})
	@PostMapping
	public ManufacturerDto create(@RequestBody @Valid ManufacturerCreateDto manufacturerDto) {
		return manufacturerFacade.create(manufacturerDto);
	}
	@Operation(
			summary = "Update manufacturer by ID",
			description = "Updates the manufacturer with the specified ID.")
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "The manufacturer was updated."),
			@ApiResponse(responseCode = "404", description = "The manufacturer with the specified ID was not found."),
			@ApiResponse(responseCode = "400", description = "The request was malformed.")
	})
    @PutMapping("/{id}")
    public ManufacturerDto updateById(@PathVariable @Parameter(description = "The id of the manufacturer.") String id,
                                  @RequestBody @Valid ManufacturerUpdateDto manufacturerUpdateDto) {
        return manufacturerFacade.updateById(manufacturerUpdateDto, id);
    }
	@Operation(
			summary = "Delete manufacturer",
			description = "Deletes the manufacturer with the specified ID.",
			tags = {"manufacturer"})
	@ApiResponses(value = {
			@ApiResponse(responseCode = "204", description = "Manufacturer deleted successfully"),
			@ApiResponse(responseCode = "404", description = "Manufacturer not found"),
			@ApiResponse(responseCode = "500", description = "Internal server error")
	})
	@DeleteMapping("/{id}")
    public ManufacturerDto deleteById(
            @PathVariable @Parameter(description = "The id of the manufacturer.") String id) {
        return manufacturerFacade.deleteById(id);
    }
}
 No newline at end of file
Loading