diff --git a/core/src/main/java/cz/muni/fi/pa165/core/DataInitializer.java b/core/src/main/java/cz/muni/fi/pa165/core/DataInitializer.java index 7fbbb746b565e9b528f57ef3604cc0d587039bf0..7a0bdc5ec0c8bae4609372dab7cdc415d7bbb5a7 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/DataInitializer.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/DataInitializer.java @@ -11,6 +11,10 @@ import cz.muni.fi.pa165.core.smartmeter.SmartMeterService; import cz.muni.fi.pa165.core.user.User; import cz.muni.fi.pa165.core.user.UserService; import cz.muni.fi.pa165.core.user.UserType; +import cz.muni.fi.pa165.core.user.roles.HouseRole; +import cz.muni.fi.pa165.core.user.roles.RoleService; +import cz.muni.fi.pa165.model.dto.role.enums.HouseRoleEnum; +import cz.muni.fi.pa165.model.dto.role.enums.RoleTypeEnum; import lombok.RequiredArgsConstructor; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; @@ -28,6 +32,7 @@ public class DataInitializer implements ApplicationRunner { private final SmartMeterService smartMeterService; private final MetricsService metricsService; private final HouseService houseService; + private final RoleService roleService; @Override public void run(ApplicationArguments args) throws Exception { @@ -35,6 +40,7 @@ public class DataInitializer implements ApplicationRunner { SeedDevice(); SeedSmartMeter(); SeedMetrics(); + SeedOwnerRole(); } private void SeedUsers() { @@ -75,6 +81,12 @@ public class DataInitializer implements ApplicationRunner { .build(); userService.create(user); + HouseRole role = HouseRole.builder() + .houseRole(HouseRoleEnum.Owner) + .build(); + role.setUser(user); + roleService.create(role); + Device device = Device.builder().name("device03").build(); deviceService.create(device); @@ -83,13 +95,15 @@ public class DataInitializer implements ApplicationRunner { List<SmartMeter> smartMeterList = new ArrayList<SmartMeter>(); smartMeterList.add(smartMeter); + ArrayList<HouseRole> owners = new ArrayList<>(); + owners.add(role); House house = House.builder() .smartMeterList(smartMeterList) .address("lol") .city("lol") .state("lol") .zipcode("lol") - .user(user) + .ownerList(owners) .build(); houseService.create(house); @@ -104,4 +118,35 @@ public class DataInitializer implements ApplicationRunner { } + + private void SeedOwnerRole() + { + User user = + User.builder() + .email("test@email.com") + .firstName("John") + .lastName("Doe") + .username("johnD") + .password("password") + .userType(UserType.ADMIN) + .build(); + user.setId("6a9f853d-59ef-4636-9c0b-bbac44bfd7f4"); + userService.create(user); + + House house = House.builder() + .address("lol") + .city("lol") + .state("lol") + .zipcode("lol") + .build(); + house.setId("6aac9569-2d34-4aba-971b-57248fc34c80"); + houseService.create(house); + + HouseRole role = HouseRole.builder() + .house(house) + .build(); + role.setUser(user); + role.setRoleType(RoleTypeEnum.HouseRole); + roleService.create(role); + } } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/company/Company.java b/core/src/main/java/cz/muni/fi/pa165/core/company/Company.java index 1befe2916dc0e6672a76536b4c67417d4f0bc0de..f495e09259d16f61daff8e236ded802707963018 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/company/Company.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/company/Company.java @@ -1,7 +1,9 @@ package cz.muni.fi.pa165.core.company; import cz.muni.fi.pa165.core.common.DomainObject; +import cz.muni.fi.pa165.core.user.roles.CompanyRole; import jakarta.persistence.Entity; +import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import lombok.AllArgsConstructor; import lombok.Builder; @@ -9,6 +11,8 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import java.util.List; + @Getter @Setter @Entity @@ -18,4 +22,6 @@ import lombok.Setter; @Table(name = "domain_company") public class Company extends DomainObject { private String name; + @OneToMany + private List<CompanyRole> employeeList; } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/house/House.java b/core/src/main/java/cz/muni/fi/pa165/core/house/House.java index 3578e107261712e51ddd7f75adcbfad274ae7d89..32da728fd1907ac94997a7ce0023056c6df8f0f0 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/house/House.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/house/House.java @@ -2,11 +2,8 @@ package cz.muni.fi.pa165.core.house; import cz.muni.fi.pa165.core.common.DomainObject; import cz.muni.fi.pa165.core.smartmeter.SmartMeter; -import cz.muni.fi.pa165.core.user.User; -import jakarta.persistence.Entity; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Table; +import cz.muni.fi.pa165.core.user.roles.HouseRole; +import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -32,6 +29,6 @@ public class House extends DomainObject { @OneToMany private List<SmartMeter> smartMeterList; - @ManyToOne - private User user; + @OneToMany + private List<HouseRole> ownerList; } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/User.java b/core/src/main/java/cz/muni/fi/pa165/core/user/User.java index f27f8c24fcd0705d775c0d8cde768ba35c342b69..fddbffe18e20539c0d145e6da7040f85021de1c6 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/User.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/User.java @@ -2,6 +2,7 @@ package cz.muni.fi.pa165.core.user; import cz.muni.fi.pa165.core.common.DomainObject; import cz.muni.fi.pa165.core.house.House; +import cz.muni.fi.pa165.core.user.roles.Role; import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; @@ -38,5 +39,5 @@ public class User extends DomainObject { private String lastName; @OneToMany - private List<House> housesList; + private List<Role> rolesList; } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/UserController.java b/core/src/main/java/cz/muni/fi/pa165/core/user/UserController.java index f4b7c25812c8bf0ee7f7206fe7bbabefde9812c9..56bd7cb344f1f31fcd26ac45a8231eb553a98bd2 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/UserController.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/UserController.java @@ -140,9 +140,9 @@ public class UserController { @ApiResponse(responseCode = "200", description = "Success"), @ApiResponse(responseCode = "404", description = "User not found"), }) - public List<Double> userHouseStatistics( + public Double userHouseStatistics( @RequestBody UserStatisticsCreateDto createStatDto) { - return userService.getConsumption(createStatDto.getId(), createStatDto.getHouseId(), + return userService.getConsumption(createStatDto.getUserId(), createStatDto.getHouseId(), createStatDto.getStartTime(), createStatDto.getEndTime()); } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/UserRepository.java b/core/src/main/java/cz/muni/fi/pa165/core/user/UserRepository.java index 2526372ec8950faab64cbc8357502337ca407423..2bd0ba41ef3b77fb3b8e6029851117a827a80d3a 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/UserRepository.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/UserRepository.java @@ -18,14 +18,15 @@ public interface UserRepository extends JpaRepository<User, String> { int update(@Param("user") User user, @Param("id") String id); - @Query("Select m.consumptionKWH " + + @Query("Select SUM(m.consumptionKWH) " + "From User u " + + "INNER JOIN HouseRole hr " + "INNER JOIN House h " + "INNER JOIN SmartMeter s " + "INNER JOIN Metrics m " + "where u.id = :#{#userId} and h.id = :#{#houseId} and m.timeStamp >= :#{#startTime} and m.timeStamp <= :#{#endTime}") - List<Double> getStatisticsData(@Param("userId") String userId, + Double getStatisticsData(@Param("userId") String userId, @Param("houseId") String houseId, @Param("startTime") LocalDateTime startTime, - @Param("startTime") LocalDateTime endTime); + @Param("endTime") LocalDateTime endTime); } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java b/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java index f20afdfd4734929e2dcfae30ded8319560580bae..eb39e986567c7c51e761a12a14e11a4f73445585 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java @@ -30,7 +30,7 @@ public class UserService extends DomainService<User> { } @Transactional(readOnly = true) - public List<Double> getConsumption(String userId, + public Double getConsumption(String userId, String houseId, LocalDateTime startTime, LocalDateTime endTime) { diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/AdminRole.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/AdminRole.java deleted file mode 100644 index 15c54db36ea07e585c6cbe9aa12a6d8feb4bc634..0000000000000000000000000000000000000000 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/AdminRole.java +++ /dev/null @@ -1,15 +0,0 @@ -package cz.muni.fi.pa165.core.user.roles; - -import jakarta.persistence.Entity; -import jakarta.persistence.Table; -import lombok.*; - -@Getter -@Setter -@Entity -@Builder -@NoArgsConstructor -@Table(name = "domain_admin") -public class AdminRole extends Role{ - -} diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/EmployeeRole.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/CompanyRole.java similarity index 58% rename from core/src/main/java/cz/muni/fi/pa165/core/user/roles/EmployeeRole.java rename to core/src/main/java/cz/muni/fi/pa165/core/user/roles/CompanyRole.java index dc8918aedbf4cf913faa75ff04526f4693afa8df..5612ac5f9a7ab1fbfb593492a7ac43a75a545636 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/EmployeeRole.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/CompanyRole.java @@ -1,6 +1,7 @@ package cz.muni.fi.pa165.core.user.roles; import cz.muni.fi.pa165.core.company.Company; +import cz.muni.fi.pa165.model.dto.role.enums.CompanyRoleEnum; import jakarta.persistence.Entity; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; @@ -12,8 +13,9 @@ import lombok.*; @Builder @NoArgsConstructor @AllArgsConstructor -@Table(name = "domain_employee") -public class EmployeeRole extends Role{ +@Table(name = "domain_companyRole") +public class CompanyRole extends Role{ @ManyToOne - public Company company; + private Company company; + private CompanyRoleEnum companyRoleEnum; } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/HouseRole.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/HouseRole.java new file mode 100644 index 0000000000000000000000000000000000000000..bed04d0306bae34a1689de05b8b6391a7fcf41a7 --- /dev/null +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/HouseRole.java @@ -0,0 +1,21 @@ +package cz.muni.fi.pa165.core.user.roles; + +import cz.muni.fi.pa165.core.house.House; +import cz.muni.fi.pa165.model.dto.role.enums.HouseRoleEnum; +import jakarta.persistence.*; +import lombok.*; + +@Getter +@Setter +@Entity +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Table(name = "domain_houseRoles") +public class HouseRole extends Role { + + @ManyToOne + private House house; + + private HouseRoleEnum houseRole; +} diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/OwnerRole.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/OwnerRole.java deleted file mode 100644 index b6582e6fe432dadb59eca1418510049cbf019cc2..0000000000000000000000000000000000000000 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/OwnerRole.java +++ /dev/null @@ -1,19 +0,0 @@ -package cz.muni.fi.pa165.core.user.roles; - -import cz.muni.fi.pa165.core.house.House; -import jakarta.persistence.Entity; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.Table; -import lombok.*; - -@Getter -@Setter -@Entity -@Builder -@NoArgsConstructor -@AllArgsConstructor -@Table(name = "domain_owner") -public class OwnerRole extends Role{ - @ManyToOne - public House house; -} diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/Role.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/Role.java index 837b99bb677f7da649b30d6b7769c9d381d624d1..c6c112bb35c579900b75204265dca47416d71f67 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/Role.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/Role.java @@ -2,30 +2,22 @@ package cz.muni.fi.pa165.core.user.roles; import cz.muni.fi.pa165.core.common.DomainObject; import cz.muni.fi.pa165.core.user.User; -import cz.muni.fi.pa165.model.dto.user.UserCreateDto; -import cz.muni.fi.pa165.model.dto.user.UserDto; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.media.Content; -import io.swagger.v3.oas.annotations.media.Schema; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.responses.ApiResponses; -import jakarta.persistence.Entity; -import jakarta.persistence.Inheritance; -import jakarta.persistence.InheritanceType; -import jakarta.persistence.ManyToOne; +import cz.muni.fi.pa165.model.dto.role.enums.RoleTypeEnum; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; @Getter @Setter @Entity +@NoArgsConstructor +@AllArgsConstructor @Inheritance(strategy = InheritanceType.JOINED) -public abstract class Role extends DomainObject { - +public class Role extends DomainObject { @ManyToOne - public User user; - private RoleTypeEnum roleType; + @JoinColumn(name= "owner_id") + protected User user; + protected RoleTypeEnum roleType; } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleController.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleController.java index 9be35953c2892a686831d4d553f36587777e7495..1c57f304bfa1595f5268694089f0b162f4f524c4 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleController.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleController.java @@ -1,8 +1,9 @@ package cz.muni.fi.pa165.core.user.roles; import cz.muni.fi.pa165.model.dto.common.Result; +import cz.muni.fi.pa165.model.dto.role.CompanyRoleDto; +import cz.muni.fi.pa165.model.dto.role.HouseRoleDto; import cz.muni.fi.pa165.model.dto.role.RoleDto; -import cz.muni.fi.pa165.model.dto.user.UserCreateDto; import cz.muni.fi.pa165.model.dto.user.UserDto; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -11,23 +12,24 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import javax.swing.*; import java.util.List; @RestController @RequestMapping("/api/role") public class RoleController { - private final RoleService roleService; + private final RoleService roleService; + private final RoleFacade roleFacade; @Autowired - public RoleController(RoleService roleService){ + public RoleController(RoleService roleService, RoleFacade roleFacade){ this.roleService = roleService; + this.roleFacade = roleFacade; } + @Operation( - summary = "Create role for user", + summary = "Create house role", description = "Creates a new user roles", tags = {"role"}) @ApiResponses( @@ -42,43 +44,68 @@ public class RoleController { }), @ApiResponse(responseCode = "400", description = "Invalid input", content = @Content), }) - @PostMapping - public Role create( - @Parameter(description = "Role to be created") @RequestBody Role roleDto) { - return roleService.create(roleDto); + @PostMapping("/house-role") + public HouseRoleDto createHouseRole( + @Parameter(description = "Role to be created") @RequestBody HouseRoleDto roleDto) { + return roleFacade.createHouseRole(roleDto); } - @DeleteMapping("/{id}") - @Operation(summary = "Delete a user role") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "Role deleted successfully"), - @ApiResponse(responseCode = "404", description = "Role not found"), - @ApiResponse(responseCode = "500", description = "Internal server error") - }) - public Role deleteById( - @Parameter(description = "ID of the role to be deleted") @PathVariable String id) { - return roleService.deleteById(id); + @Operation( + summary = "Create company role", + description = "Creates a new user roles", + tags = {"role"}) + @ApiResponses( + value = { + @ApiResponse( + responseCode = "201", + description = "Role created", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = UserDto.class)) + }), + @ApiResponse(responseCode = "400", description = "Invalid input", content = @Content), + }) + @PostMapping("/company-role") + public CompanyRoleDto createCompanyRole( + @Parameter(description = "Role to be created") @RequestBody CompanyRoleDto roleDto) { + return roleFacade.createCompanyRole(roleDto); + //return new CompanyRole(); } @Operation( - summary = "Get all user roles", - description = "Returns all user roles", + summary = "Create general role", + description = "Creates a new user roles", tags = {"role"}) @ApiResponses( value = { @ApiResponse( - responseCode = "200", - description = "Roles found", + responseCode = "201", + description = "Role created", content = { @Content( mediaType = "application/json", - schema = @Schema(implementation = Result.class)) - }) + schema = @Schema(implementation = UserDto.class)) + }), + @ApiResponse(responseCode = "400", description = "Invalid input", content = @Content), }) - @GetMapping - public List<Role> findUserRoles( - @Parameter(description = "Page number of results to retrieve") @RequestParam String userId) { - return roleService.findAllByUser(userId); + @PostMapping("/general-role") + public RoleDto createGeneralRole( + @Parameter(description = "Role to be created") @RequestBody RoleDto roleDto) { + return roleFacade.createGeneralRole(roleDto); + } + + + @DeleteMapping("/{id}") + @Operation(summary = "Delete a user role") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Role deleted successfully"), + @ApiResponse(responseCode = "404", description = "Role not found"), + @ApiResponse(responseCode = "500", description = "Internal server error") + }) + public RoleDto deleteById( + @Parameter(description = "ID of the role to be deleted") @PathVariable String id) { + return roleFacade.deleteRole(id); } @Operation( @@ -96,9 +123,9 @@ public class RoleController { schema = @Schema(implementation = Result.class)) }) }) - @GetMapping("/all") - public List<Role> findAll( + @GetMapping + public List<RoleDto> findUserRoles( @Parameter(description = "Page number of results to retrieve") @RequestParam String userId) { - return roleService.findAll(); + return roleService.findAllByUser(userId); } } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleFacade.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleFacade.java new file mode 100644 index 0000000000000000000000000000000000000000..b490dc5722e607b33d5da35ee2e696cc7ee63ecb --- /dev/null +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleFacade.java @@ -0,0 +1,71 @@ +package cz.muni.fi.pa165.core.user.roles; + +import cz.muni.fi.pa165.core.company.CompanyService; +import cz.muni.fi.pa165.core.house.HouseService; +import cz.muni.fi.pa165.core.user.UserService; +import cz.muni.fi.pa165.model.dto.role.CompanyRoleDto; +import cz.muni.fi.pa165.model.dto.role.HouseRoleDto; +import cz.muni.fi.pa165.model.dto.role.RoleDto; +import cz.muni.fi.pa165.model.dto.role.enums.RoleTypeEnum; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class RoleFacade { + + private final RoleService roleService; + private final HouseService houseService; + private final UserService userService; + private final CompanyService companyService; + private final RoleMapper mapper; + + @Autowired + public RoleFacade(RoleService service, HouseService houseService, UserService userService, CompanyService companyService, RoleMapper mapper) { + this.roleService = service; + this.houseService = houseService; + this.userService = userService; + this.companyService = companyService; + this.mapper = mapper; + } + + public HouseRoleDto createHouseRole(HouseRoleDto entity){ + HouseRole houseRole = mapper.fromHouseDto(entity); + houseRole.setHouse(houseService.findById(entity.getHouseId())); + houseRole.setUser(userService.findById(entity.getUserId())); + // TODO: how to mapp parent properties? + houseRole.setHouseRole(entity.getHouseRole()); + houseRole.setRoleType(RoleTypeEnum.HouseRole); + + return mapper.toHouseDto((HouseRole) roleService.create(houseRole)); + } + + public CompanyRoleDto createCompanyRole(CompanyRoleDto entity){ + CompanyRole companyRole = mapper.fromCompanyDto(entity); + companyRole.setCompany(companyService.findById(entity.getCompanyId())); + companyRole.setUser(userService.findById(entity.getUserId())); + // TODO: how to mapp parent properties? + companyRole.setCompanyRoleEnum(entity.getCompanyRole()); + companyRole.setRoleType(RoleTypeEnum.CompanyRole); + + return mapper.toCompanyDto((CompanyRole) roleService.create(companyRole)); + } + + public RoleDto createGeneralRole(RoleDto entity){ + Role role = mapper.fromGeneralDto(entity); + role.setUser(userService.findById(entity.getUserId())); + // TODO: how to mapp parent properties? + role.setRoleType(entity.getRoleType()); + + return mapper.toGeneralDto((Role) roleService.create(role)); + } + + public RoleDto deleteRole(String roleId){ + return mapper.toGeneralDto((Role) roleService.deleteById(roleId)); + } + + public List<RoleDto> findAllByUser(String userId){ + return mapper.toGeneralListDto(roleService.findAllByUser(userId)); + } +} diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleMapper.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleMapper.java index a1600f114cd4a228b72cc570f5385ebe755c79f9..0221f3e9d8566929f532b3fa328dbe074d435453 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleMapper.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleMapper.java @@ -1,30 +1,19 @@ package cz.muni.fi.pa165.core.user.roles; -import cz.muni.fi.pa165.core.common.DomainMapper; -import cz.muni.fi.pa165.core.smartmeter.SmartMeter; -import cz.muni.fi.pa165.model.dto.role.RoleDto; -import cz.muni.fi.pa165.model.dto.role.RoleCreateDto; -import cz.muni.fi.pa165.model.dto.role.RoleUpdateDto; -import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterCreateDto; -import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterDto; -import org.mapstruct.Mapping; +import cz.muni.fi.pa165.model.dto.role.*; +import org.mapstruct.Mapper; import java.util.List; -public interface RoleMapper extends DomainMapper<Role, RoleDto, RoleCreateDto, RoleUpdateDto> { - /* - @Mapping(source = "deviceDto", target = "device") - SmartMeter fromCreateDto(SmartMeterCreateDto dto); +@Mapper +public interface RoleMapper{ + HouseRole fromHouseDto(HouseRoleDto entity); + CompanyRole fromCompanyDto(CompanyRoleDto entity); + Role fromGeneralDto(RoleDto entity); - //@Mapping(source = "deviceDto", target = "device") - //SmartMeter fromUpdateDto(SmartMeterUpdateDto dto); + HouseRoleDto toHouseDto(HouseRole entity); + CompanyRoleDto toCompanyDto(CompanyRole entity); + RoleDto toGeneralDto(Role entity); - @Mapping(source = "roleDto", target = "role") - SmartMeter fromDto(SmartMeterDto dto); - - @Mapping(source = "device", target = "deviceDto") - SmartMeterDto toDto(SmartMeter entity); - - /*@Mapping(source = "device", target = "deviceDto") - List<SmartMeterDto> toDtoList(List<SmartMeter> entities);*/ + List<RoleDto> toGeneralListDto(List<Role> entities); } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleRepository.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleRepository.java index 048ca18b6f6bf5da5d95421bcfb4b0a52bd174dd..23e6ff4d6822fccbe937780cca53f634ec622bb8 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleRepository.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleRepository.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Repository; import java.util.List; @Repository -public interface RoleRepository extends JpaRepository<Role, String> { +public interface RoleRepository<T extends Role> extends JpaRepository<T, String> { - public List<Role> findAllByUser(String userId); + public List<T> findAllByUserId(String userId); } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleService.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleService.java index dd6bc3b49853bb8f4d4f12fde80da25d9f2bc479..713eb11be795551a5e8a1e7c102bf8196adfaee2 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleService.java @@ -1,22 +1,18 @@ package cz.muni.fi.pa165.core.user.roles; -import cz.muni.fi.pa165.core.common.DomainObject; import cz.muni.fi.pa165.core.common.DomainService; -import cz.muni.fi.pa165.model.dto.role.RoleDto; import lombok.Getter; import lombok.Setter; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.time.LocalDateTime; import java.util.List; @Getter @Setter @Service -public class RoleService extends DomainService<Role> { +public class RoleService<T extends Role> extends DomainService<T> { @Getter private final RoleRepository repository; @@ -28,6 +24,6 @@ public class RoleService extends DomainService<Role> { @Transactional public List<Role> findAllByUser(String id){ - return repository.findAllByUser(id); + return repository.findAllByUserId(id); } } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleTypeEnum.java b/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleTypeEnum.java deleted file mode 100644 index 8fbc793338c1abb37e27097d348f45a00300a0e1..0000000000000000000000000000000000000000 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/roles/RoleTypeEnum.java +++ /dev/null @@ -1,7 +0,0 @@ -package cz.muni.fi.pa165.core.user.roles; - -public enum RoleTypeEnum { - Admin, - Employee, - Owner -} diff --git a/core/src/main/resources/application.properties b/core/src/main/resources/application.properties index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..69b89983cb2b54a75be402a7d8ff5f4c9cd4c069 100644 --- a/core/src/main/resources/application.properties +++ b/core/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.h2.console.enabled=true \ No newline at end of file diff --git a/core/src/main/resources/application.yml b/core/src/main/resources/application.yml index f062a9f94775452e30c68582e659bd880312bd91..53c22466a95dbb04615999faf6781494d7c5a309 100644 --- a/core/src/main/resources/application.yml +++ b/core/src/main/resources/application.yml @@ -13,6 +13,15 @@ logging: spring: mvc: log-request-details: true + datasource: + url: jdbc:h2:mem:mydb + username: sa + password: password + driverClassName: org.h2.Driver + jpa: + spring.jpa.database-platform: org.hibernate.dialect.H2Dialect + h2: + console.enabled: true springdoc: # https://springdoc.org/properties.html#_springdoc_openapi_core_properties diff --git a/core/src/test/java/cz/muni/fi/pa165/core/company/UnitTestCompanyJPA.java b/core/src/test/java/cz/muni/fi/pa165/core/company/UnitTestCompanyJPA.java index 247af80a49110f0d675304482527a640f9384d38..db3709cbddb79e6cb2bead3b28b1d07060235a68 100644 --- a/core/src/test/java/cz/muni/fi/pa165/core/company/UnitTestCompanyJPA.java +++ b/core/src/test/java/cz/muni/fi/pa165/core/company/UnitTestCompanyJPA.java @@ -10,6 +10,7 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; import org.springframework.test.context.junit4.SpringRunner; +import java.util.ArrayList; import java.util.regex.Pattern; import static org.assertj.core.api.Assertions.assertThat; @@ -46,7 +47,7 @@ public class UnitTestCompanyJPA { } private Company helperRegister() { - return companyRepository.save(new Company("Tesla")); + return companyRepository.save(new Company("Tesla", new ArrayList<>())); } @Test diff --git a/core/src/test/java/cz/muni/fi/pa165/core/house/UnitTestHouseJPA.java b/core/src/test/java/cz/muni/fi/pa165/core/house/UnitTestHouseJPA.java index c2af8041acf75a57735a47393bf2c6cc1988ade5..2c0ac0a1633bcc4673d1f5e01524c610b56e9af1 100644 --- a/core/src/test/java/cz/muni/fi/pa165/core/house/UnitTestHouseJPA.java +++ b/core/src/test/java/cz/muni/fi/pa165/core/house/UnitTestHouseJPA.java @@ -10,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -29,7 +31,7 @@ public class UnitTestHouseJPA { @Autowired private UserRepository userRepository; - @Test + @Test public void createHouseTesting() throws Exception { String house = this.entityManager.persistAndGetId(new House()).toString(); // Regular expression to match UUID format @@ -55,7 +57,7 @@ public class UnitTestHouseJPA { .collect(Collectors.toList()); //need to save a user to the database first to asign him to the new house User user = userRepository.save(new User()); - return houseRepository.save(new House("Titova 6", "Sarajevo", "Bosnia and Herzegovina", "71000", list, user)); + return houseRepository.save(new House("Titova 6", "Sarajevo", "Bosnia and Herzegovina", "71000", list, new ArrayList<>())); } @Test @@ -121,7 +123,7 @@ public class UnitTestHouseJPA { assertThat(checkHouse.getId()).isEqualTo(house2.getId()); assertThat(checkHouse.getAddress()).isEqualTo(house2.getAddress()); - assertThat(checkHouse.getUser()).isEqualTo(house2.getUser()); + //assertThat(checkHouse.getUser()).isEqualTo(house2.getUser()); assertThat(checkHouse.getCity()).isEqualTo(house2.getCity()); } diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsCreateDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsCreateDto.java index 0c7fe3d71579c44dafaf4875329e82148f2cbf7c..5884c44ffd86481afffc74f5f84d62d5d9950beb 100644 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsCreateDto.java +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsCreateDto.java @@ -1,7 +1,11 @@ package cz.muni.fi.pa165.model.dto.metrics; import cz.muni.fi.pa165.model.dto.common.DomainObjectDto; +import lombok.Getter; +import lombok.Setter; +@Getter +@Setter public class MetricsCreateDto extends DomainObjectDto { private double consumptionKWH; } diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsDto.java index 20c1ddca95de6060325cfb549b646dfb57c32e08..2907ffa1a50d5f4e33ca394f665dde5deffa9a21 100644 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsDto.java +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsDto.java @@ -1,7 +1,11 @@ package cz.muni.fi.pa165.model.dto.metrics; import cz.muni.fi.pa165.model.dto.common.DomainObjectDto; +import lombok.Getter; +import lombok.Setter; +@Getter +@Setter public class MetricsDto extends DomainObjectDto { private double consumptionKWH; } diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsUpdateDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsUpdateDto.java index 99556ef0be0ca3b9acd7301e44f7d9f335e349bd..d4904ba92d39a35adf99673a8b098c2136fa7b34 100644 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsUpdateDto.java +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/metrics/MetricsUpdateDto.java @@ -1,7 +1,11 @@ package cz.muni.fi.pa165.model.dto.metrics; import cz.muni.fi.pa165.model.dto.common.DomainObjectDto; +import lombok.Getter; +import lombok.Setter; +@Getter +@Setter public class MetricsUpdateDto extends DomainObjectDto { private double consumptionKWH; } diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/CompanyRoleDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/CompanyRoleDto.java new file mode 100644 index 0000000000000000000000000000000000000000..208f7ef18812348c00974e9ae2ea7cf4e1823cb8 --- /dev/null +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/CompanyRoleDto.java @@ -0,0 +1,15 @@ +package cz.muni.fi.pa165.model.dto.role; + +import cz.muni.fi.pa165.model.dto.role.enums.CompanyRoleEnum; +import cz.muni.fi.pa165.model.dto.role.enums.RoleTypeEnum; +import lombok.*; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class CompanyRoleDto extends RoleDto{ + private String CompanyId; + private CompanyRoleEnum companyRole; + private RoleTypeEnum roleType = RoleTypeEnum.CompanyRole; +} diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/EmployeeRoleDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/EmployeeRoleDto.java deleted file mode 100644 index e76b691a1c31ecb4d221796561d51e94074fa9f3..0000000000000000000000000000000000000000 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/EmployeeRoleDto.java +++ /dev/null @@ -1,8 +0,0 @@ -package cz.muni.fi.pa165.model.dto.role; - -public class EmployeeRoleDto extends RoleDto{ - /* - private CompanyDto company; / companyId - - */ -} diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/HouseRoleDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/HouseRoleDto.java new file mode 100644 index 0000000000000000000000000000000000000000..23e90429f7642e1440c82b30e245f424746d637a --- /dev/null +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/HouseRoleDto.java @@ -0,0 +1,15 @@ +package cz.muni.fi.pa165.model.dto.role; + +import cz.muni.fi.pa165.model.dto.role.enums.HouseRoleEnum; +import cz.muni.fi.pa165.model.dto.role.enums.RoleTypeEnum; +import lombok.*; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class HouseRoleDto extends RoleDto{ + private String houseId; + private HouseRoleEnum houseRole; + private RoleTypeEnum roleType = RoleTypeEnum.HouseRole; +} diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/OwnerRoleDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/OwnerRoleDto.java deleted file mode 100644 index 2a57a918c03d18f198fa19ae72d407e2906dfdc3..0000000000000000000000000000000000000000 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/OwnerRoleDto.java +++ /dev/null @@ -1,11 +0,0 @@ -package cz.muni.fi.pa165.model.dto.role; - -import cz.muni.fi.pa165.model.dto.house.HouseDto; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class OwnerRoleDto extends RoleDto{ - private HouseDto houseDto; -} diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleCreateDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleCreateDto.java index 60decb28c6051556d05d0c90bb8038594e838e19..86e54409ee5de0edf1735ae552375b79e66f8aac 100644 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleCreateDto.java +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleCreateDto.java @@ -1,6 +1,7 @@ package cz.muni.fi.pa165.model.dto.role; import cz.muni.fi.pa165.model.dto.common.DomainObjectDto; +import cz.muni.fi.pa165.model.dto.role.enums.RoleTypeEnum; public class RoleCreateDto extends DomainObjectDto { RoleTypeEnum roleType; diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleDto.java index 423636a005204ac8e17808252729bdb76f2cbf7a..eb8026711e2265ee65c9d5ee8b4e2933554e1f40 100644 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleDto.java +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleDto.java @@ -1,8 +1,14 @@ package cz.muni.fi.pa165.model.dto.role; import cz.muni.fi.pa165.model.dto.common.DomainObjectDto; +import cz.muni.fi.pa165.model.dto.role.enums.RoleTypeEnum; +import lombok.*; +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor public class RoleDto extends DomainObjectDto { - RoleTypeEnum roleType; - String userId; + protected String userId; + protected RoleTypeEnum roleType; } diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleTypeEnum.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleTypeEnum.java deleted file mode 100644 index 678d1c29b77cf5a877bd31ed4e1600b7aa57da10..0000000000000000000000000000000000000000 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleTypeEnum.java +++ /dev/null @@ -1,7 +0,0 @@ -package cz.muni.fi.pa165.model.dto.role; - -public enum RoleTypeEnum { - Admin, - Employee, - User -} diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleUpdateDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleUpdateDto.java index c1a434ebe181cd2a25d1344e2b5e3a67ac1a04d3..79c77496b37d3b69dae5ec61b86e9f98b74ed0c8 100644 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleUpdateDto.java +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/RoleUpdateDto.java @@ -1,6 +1,7 @@ package cz.muni.fi.pa165.model.dto.role; import cz.muni.fi.pa165.model.dto.common.DomainObjectDto; +import cz.muni.fi.pa165.model.dto.role.enums.RoleTypeEnum; public class RoleUpdateDto extends DomainObjectDto { RoleTypeEnum roleType; diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/enums/CompanyRoleEnum.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/enums/CompanyRoleEnum.java new file mode 100644 index 0000000000000000000000000000000000000000..aec67ab0ab795affa9e17a1118b905b5b0d0186e --- /dev/null +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/enums/CompanyRoleEnum.java @@ -0,0 +1,5 @@ +package cz.muni.fi.pa165.model.dto.role.enums; + +public enum CompanyRoleEnum { + Employee +} diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/enums/HouseRoleEnum.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/enums/HouseRoleEnum.java new file mode 100644 index 0000000000000000000000000000000000000000..8e3f9914e2e2170316f6133aa92b7e7cbef2472b --- /dev/null +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/enums/HouseRoleEnum.java @@ -0,0 +1,5 @@ +package cz.muni.fi.pa165.model.dto.role.enums; + +public enum HouseRoleEnum { + Owner +} diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/role/enums/RoleTypeEnum.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/enums/RoleTypeEnum.java new file mode 100644 index 0000000000000000000000000000000000000000..739caff808be8ccba9752c9f8060ba52f6f1eb2f --- /dev/null +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/role/enums/RoleTypeEnum.java @@ -0,0 +1,7 @@ +package cz.muni.fi.pa165.model.dto.role.enums; + +public enum RoleTypeEnum { + Admin, + HouseRole, + CompanyRole, +} diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/user/UserStatisticsCreateDto.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/user/UserStatisticsCreateDto.java index 098d93430030af610250b3d621d98cb2fa806569..c7b405f675b2fe5a4f7029b4cccc1c26b624210f 100644 --- a/model/src/main/java/cz/muni/fi/pa165/model/dto/user/UserStatisticsCreateDto.java +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/user/UserStatisticsCreateDto.java @@ -9,7 +9,7 @@ import java.time.LocalDateTime; @Setter public class UserStatisticsCreateDto { - private String id; + private String userId; private String houseId; private LocalDateTime startTime; private LocalDateTime endTime; diff --git a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticCreateDto.java b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticCreateDto.java index 484ed505f017f884deed133b368a42495f8405cb..90ef59ec270d0a1fb9e7e642178c1ee56b3993b5 100644 --- a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticCreateDto.java +++ b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticCreateDto.java @@ -18,7 +18,6 @@ import java.util.Date; @Builder @Data public class StatisticCreateDto { - private String id; private String userId; @Email private String userEmail; diff --git a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticRepository.java b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticRepository.java index f8c79bbb8d07da26e5a23bf945b2c7aeb7cbf39d..7c5df6b66be1df76c847a8da821342980d0c56ef 100644 --- a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticRepository.java +++ b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticRepository.java @@ -11,12 +11,12 @@ import java.util.List; @Repository public interface StatisticRepository extends JpaRepository<UserStatistic, String> { - @Query("select name, userId, monthNum, yearNum, monthlyConsumption from UserStatistic where userId = :#{#userId}") + @Query("select new UserStatistic (u.id, u.userId, u.userEmail, u.houseId, u.monthNum, u.yearNum, u.monthlyConsumption, u.name) from UserStatistic u where u.userId = :#{#userId}") List<UserStatistic> findAllByUser(@Param("userId") String userId); - @Query("select id, name, userId, monthNum, yearNum, monthlyConsumption from UserStatistic where userId = :#{#userId} and houseId = :#{#houseId}") + @Query("select new UserStatistic (u.id, u.userId, u.userEmail, u.houseId, u.monthNum, u.yearNum, u.monthlyConsumption, u.name) from UserStatistic u where u.userId = :#{#userId} and u.houseId = :#{#houseId}") List<UserStatistic> findAllByUserAndHouse(@Param("userId") String userId, @Param("houseId") String houseId); - @Query("select id, name, userId, monthNum, yearNum, monthlyConsumption from UserStatistic where userId = :#{#userId} and houseId = :#{#houseId} and monthNum = :#{#month} and yearNum = :#{#year}") + @Query("select new UserStatistic(u.id, u.userId, u.userEmail, u.houseId, u.monthNum, u.yearNum, u.monthlyConsumption, u.name) from UserStatistic u where u.userId = :#{#userId} and u.houseId = :#{#houseId} and u.monthNum = :#{#month} and u.yearNum = :#{#year}") UserStatistic findUserHouseDateStatistics(@Param("userId") String userId, @Param("houseId") String houseId, @Param("month") int month, @Param("year") int year); } diff --git a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticService.java b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticService.java index ca349d7c01d79deade0bdf0cb23d3316a5ae1b50..cd61ce68ba209ec860a2ed4d84d2b72c5af2612a 100644 --- a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticService.java +++ b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticService.java @@ -7,6 +7,7 @@ import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -39,6 +40,11 @@ public class StatisticService { if (statistic == null){ userStatistic.setId(UUID.randomUUID().toString()); + userStatistic.setName(String.format("User%s_house%s_%d/%d.", + userStatistic.getUserEmail(), + userStatistic.getHouseId(), + userStatistic.getMonthNum(), + userStatistic.getYearNum())); userStatistic.setMonthlyConsumption(GenerateStatistic(userStatistic)); repository.save(userStatistic); return userStatistic; @@ -60,34 +66,53 @@ public class StatisticService { } private double GenerateStatistic(UserStatistic userStatistic) throws IOException { - // TODO: get first and last day of month - LocalDateTime startDateTime = LocalDateTime.of(2023,3,1,0,0,0,1); - LocalDateTime lastDateTime = LocalDateTime.of(2023,3,30,0,0,0,1); + + LocalDateTime startDateTime = GetFirstDayOfMonth(userStatistic.getYearNum(), userStatistic.getMonthNum()); + LocalDateTime lastDateTime = GetLastDayOfMonth(userStatistic.getYearNum(), userStatistic.getMonthNum()); String payload = String.format(""" { - "id": "%s", + "userId": "%s", "houseId": "%s", - "startTime": "%s" + "startTime": "%s", "endTime": "%s" } - """, userStatistic.getId(), + """, userStatistic.getUserId(), userStatistic.getHouseId(), startDateTime, lastDateTime); StringEntity entity = new StringEntity(payload, - ContentType.APPLICATION_FORM_URLENCODED); + ContentType.APPLICATION_JSON); HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost request = new HttpPost("http://localhost:8080/api/user/statistics"); request.setEntity(entity); request.setHeader("Content-Type", "application/json"); - request.setHeader("accept:", "*/*"); HttpResponse response = httpClient.execute(request); - System.out.println(response.getStatusLine().getStatusCode()); - HttpEntity content = response.getEntity(); - //System.out.println(content.); - return 0; + + // TODO: assert response equals 200 and write message if not + + String result = EntityUtils.toString(response.getEntity()); + + return result.isEmpty() ? 0 + : Double.valueOf(result); + } + + private LocalDateTime GetLastDayOfMonth(int year, int month){ + LocalDateTime date = LocalDateTime.of(year, + month,1,0,0,0,0); + date.plusMonths(1); + date.minusDays(1); + + return LocalDateTime.of(year, + month, + date.getDayOfMonth(), + 23,59,59,999999); + } + + private LocalDateTime GetFirstDayOfMonth(int year, int month){ + return LocalDateTime.of(year, + month,1,0,0,0,0); } } diff --git a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticsFacade.java b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticsFacade.java new file mode 100644 index 0000000000000000000000000000000000000000..aa0598830804fb2e0b896652647e6c12bfbaceb9 --- /dev/null +++ b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/StatisticsFacade.java @@ -0,0 +1,40 @@ +package cz.muni.fi.pa165.statistics.statistics; + +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.RequestParam; + +import java.io.IOException; +import java.util.List; + +@Component +@Transactional +public class StatisticsFacade { + + private final StatisticService statisticService; + + private final UserStatisticsMapper mapper; + @Autowired + public StatisticsFacade(StatisticService statisticService, UserStatisticsMapper mapper) { + this.statisticService = statisticService; + this.mapper = mapper; + } + + @Transactional + public UserStatisticsDto CreateStatistics(StatisticCreateDto entity) throws IOException { + return mapper.toDto(statisticService.CreateStatistic(mapper.fromDto(entity))); + } + + @Transactional + public List<UserStatisticsDto> FindAllByUser(String userId){ + return mapper.toDtoList(statisticService.findAllUserStatistics(userId)); + } + + @Transactional + public List<UserStatisticsDto> findHouseStatisticsByUser(String userId, + String houseId) { + return mapper.toDtoList(statisticService.findAllUserStatisticsByHouse(userId, houseId)); + } +} diff --git a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatistic.java b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatistic.java index abdab02467ec5ee5377e14e07bf299529857e1a2..01979e6397b1e45d26c7fc4aafc328d69dd77fba 100644 --- a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatistic.java +++ b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatistic.java @@ -27,5 +27,5 @@ public class UserStatistic { private int monthNum; private int yearNum; private Double monthlyConsumption; - private String name = String.format("User%s_house%s_%d/%d.", userEmail, houseId, monthNum, yearNum); + private String name; } diff --git a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsController.java b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsController.java index f626f0e7258243125e0c17bf26a99f5f65f755dd..bc6b196375f4c21c65a3b2ae0e57c1be92ca6283 100644 --- a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsController.java +++ b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsController.java @@ -3,7 +3,6 @@ package cz.muni.fi.pa165.statistics.statistics; import io.swagger.v3.oas.annotations.Operation; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.io.IOException; @@ -13,46 +12,36 @@ import java.util.List; @RequestMapping("/api/user-statistics") public class UserStatisticsController { - private final StatisticService statisticService; - private final UserStatisticsMapper mapper; + private final StatisticsFacade statisticsFacade; @Autowired - public UserStatisticsController(StatisticService statisticService, UserStatisticsMapper mapper) + public UserStatisticsController(StatisticsFacade statisticsFacade) { - this.statisticService = statisticService; - this.mapper = mapper; + this.statisticsFacade = statisticsFacade; } @Operation( summary = "Create statistics", description = "Creates a new statistics.") @PostMapping - public ResponseEntity create(@RequestBody @Valid StatisticCreateDto createDto) throws IOException { - /*try - {*/ - statisticService.CreateStatistic(mapper.fromDto(createDto)); - return ResponseEntity.ok().build(); - /*} - catch (Exception e) - {*/ - // return ResponseEntity.internalServerError(e.getMessage()).build(); - //} + public UserStatisticsDto create(@RequestBody @Valid StatisticCreateDto createDto) throws IOException { + return statisticsFacade.CreateStatistics(createDto); } @Operation( summary = "Get user statistics", description = "Get user statistics.") @GetMapping("/all") - public List<UserStatistic> findAllByUser(@RequestParam @Valid String userId) { - return statisticService.findAllUserStatistics(userId); + public List<UserStatisticsDto> findAllByUser(@RequestParam @Valid String userId) { + return statisticsFacade.FindAllByUser(userId); } @Operation( summary = "Get user statistics for house", description = "Get user statistics for house.") @GetMapping("/user") - public List<UserStatistic> findHouseStatisticsByUser(@RequestParam @Valid String userId, + public List<UserStatisticsDto> findHouseStatisticsByUser(@RequestParam @Valid String userId, @RequestParam @Valid String houseId) { - return statisticService.findAllUserStatistics(userId); + return statisticsFacade.findHouseStatisticsByUser(userId, houseId); } } diff --git a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsDto.java b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsDto.java new file mode 100644 index 0000000000000000000000000000000000000000..2028028310aa7dc33466630ce2aa26a6aae507b5 --- /dev/null +++ b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsDto.java @@ -0,0 +1,27 @@ +package cz.muni.fi.pa165.statistics.statistics; + +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import lombok.Builder; +import lombok.Data; +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +@Builder +@Data +public class UserStatisticsDto { + private String userId; + @Email + private String userEmail; + private String houseId; + @Min(1) + @Max(12) + private int monthNum; + @Min(2000) + @Max(2030) + private int yearNum; + private Double monthlyConsumption; +} diff --git a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsMapper.java b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsMapper.java index 670a02b8223259ede9b97b1d6d1b54e7e03da4ec..499d8b6f027e9eb2e6d7b208f30241bd916a3a0e 100644 --- a/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsMapper.java +++ b/statistics/src/main/java/cz/muni/fi/pa165/statistics/statistics/UserStatisticsMapper.java @@ -2,7 +2,12 @@ package cz.muni.fi.pa165.statistics.statistics; import org.mapstruct.Mapper; +import java.util.List; + @Mapper public interface UserStatisticsMapper{ UserStatistic fromDto(StatisticCreateDto entity); + UserStatisticsDto toDto(UserStatistic entity); + + List<UserStatisticsDto> toDtoList(List<UserStatistic> entities); }