Skip to content

Commit

Permalink
bugfix: missed address => geocoding
Browse files Browse the repository at this point in the history
bugfix: null pointer
bugfix: missed email verified and createdDate in edit enterprise user
bugfix: missed i18n sidebar
bugfix: no buildings found
  • Loading branch information
nganntqe170236 authored and thongdanghoang committed Feb 26, 2025
1 parent 132e385 commit 85ae143
Show file tree
Hide file tree
Showing 34 changed files with 423 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package greenbuildings.enterprise.adapters.geocoding;

import greenbuildings.enterprise.dtos.AddressSuggestionDTO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.text.StringSubstitutor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class GeocodingAdapter {

private final RestTemplate restTemplate;
private final GeocodingMapper mapper;

@Value("${geocoding.key}")
private String key;

@Value("${geocoding.url}")
private String url;

public AddressSuggestionDTO reverse(double latitude, double longitude) {
var requestUrl = StringSubstitutor
.replace("${url}?lat=${lat}&lon=${lon}&api_key=${key}",
Map.of("url", this.url,
"lat", latitude,
"lon", longitude,
"key", key));
var response = restTemplate.getForObject(requestUrl, GeocodingReverseResponse.class);
return mapper.toAddressSuggestion(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package greenbuildings.enterprise.adapters.geocoding;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* This DTO is intended for internal use within the adapter and should not be used outside of it.
*/
public record GeocodingAddress(
String road,
String quarter,
String suburb,
String city,
String postcode,
String country,
@JsonProperty("country_code") String countryCode
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package greenbuildings.enterprise.adapters.geocoding;

import greenbuildings.enterprise.dtos.AddressSuggestionDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.ReportingPolicy;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, componentModel = MappingConstants.ComponentModel.SPRING)
public interface GeocodingMapper {

@Mapping(target = "city", source = "address.city")
@Mapping(target = "road", source = "address.road")
@Mapping(target = "quarter", source = "address.quarter")
@Mapping(target = "suburb", source = "address.suburb")
@Mapping(target = "postcode", source = "address.postcode")
@Mapping(target = "country", source = "address.country")
@Mapping(target = "countryCode", source = "address.countryCode")
AddressSuggestionDTO toAddressSuggestion(GeocodingReverseResponse response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package greenbuildings.enterprise.adapters.geocoding;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* This DTO is intended for internal use within the adapter and should not be used outside of it.
*/
public record GeocodingReverseResponse(
String lat,
String lon,
@JsonProperty("display_name") String displayName,
GeocodingAddress address
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package greenbuildings.enterprise.dtos;

public record AddressSuggestionDTO(
String displayName,
String road,
String quarter,
String suburb,
String city,
String postcode,
String country,
String countryCode
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public record BuildingDTO(
UUID id,
int version,
@NotBlank String name,
@NotBlank String address,
@Min(0) long numberOfDevices,
@DecimalMin("-90.0") @DecimalMax("90.0") double latitude,
@DecimalMin("-180.0") @DecimalMax("180.0") double longitude
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class BuildingEntity extends AbstractAuditableEntity {
@Column(name = "name", nullable = false)
private String name;

@NotBlank
@Column(name = "address", nullable = false)
private String address;

@Column(name = "number_of_devices", nullable = false)
private long numberOfDevices;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import commons.springfw.impl.mappers.CommonMapper;
import commons.springfw.impl.securities.UserContextData;
import greenbuildings.commons.api.dto.SearchCriteriaDTO;
import greenbuildings.commons.api.dto.SearchResultDTO;
import greenbuildings.commons.api.security.UserRole;
import greenbuildings.enterprise.dtos.BuildingDTO;
import greenbuildings.enterprise.mappers.BuildingMapper;
import greenbuildings.enterprise.services.BuildingService;
import greenbuildings.enterprise.services.EnterpriseService;
import greenbuildings.commons.api.dto.SearchCriteriaDTO;
import greenbuildings.commons.api.dto.SearchResultDTO;
import greenbuildings.commons.api.security.UserRole;
import jakarta.annotation.security.RolesAllowed;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -45,7 +45,7 @@ public ResponseEntity<BuildingDTO> getBuildingById(@PathVariable UUID id) {

@PostMapping("/search")
public ResponseEntity<SearchResultDTO<BuildingDTO>> searchEnterpriseBuildings(@RequestBody SearchCriteriaDTO<Void> searchCriteria,
@AuthenticationPrincipal UserContextData userContextData) {
@AuthenticationPrincipal UserContextData userContextData) {
var enterpriseIdFromContext = Objects.requireNonNull(userContextData.getEnterpriseId());
var pageable = CommonMapper.toPageable(searchCriteria.page(), searchCriteria.sort());
var searchResults = buildingService.getEnterpriseBuildings(enterpriseIdFromContext, pageable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package greenbuildings.enterprise.rest;

import greenbuildings.enterprise.adapters.geocoding.GeocodingAdapter;
import greenbuildings.enterprise.dtos.AddressSuggestionDTO;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/geocoding")
@RequiredArgsConstructor
public class GeocodingResource {
private final GeocodingAdapter geocodingAdapter;

@GetMapping("/reverse")
public ResponseEntity<AddressSuggestionDTO> reverse(@RequestParam @NotNull Double latitude,
@RequestParam @NotNull Double longitude) {
return ResponseEntity.ok(geocodingAdapter.reverse(latitude, longitude));
}

}
6 changes: 5 additions & 1 deletion sep490-enterprise/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ payment:

exchangerate-api:
key: ${EXCHANGERATE_API_KEY}
url: https://v6.exchangerate-api.com/v6/
url: https://v6.exchangerate-api.com/v6/

geocoding:
key: ${GEOCODING_API_KEY}
url: https://geocode.maps.co/reverse
6 changes: 5 additions & 1 deletion sep490-enterprise/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ payment:

exchangerate-api:
key: secret
url: https://v6.exchangerate-api.com/v6/
url: https://v6.exchangerate-api.com/v6/

geocoding:
key: secret
url: https://geocode.maps.co/reverse
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE buildings
ADD address VARCHAR(255) NOT NULL DEFAULT '';
3 changes: 3 additions & 0 deletions sep490-frontend/src/app/app-routing.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ export class AppRoutingConstants {
public static readonly DASHBOARD_PATH = 'dashboard';
public static readonly FORBIDDEN = 'forbidden';
public static readonly UNAUTHORIZED = 'unauthorized';

// Third party
public static readonly GEOCODING_PATH = 'geocoding';
}
11 changes: 7 additions & 4 deletions sep490-frontend/src/app/components/header/header.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
/>

@if (authenticated | async) {
<p-avatar
image="https://primefaces.org/cdn/primeng/images/demo/avatar/amyelsner.png"
class="mr-2"
shape="circle"
<img
class="rounded-full"
ngSrc="assets/images/avatar.jpeg"
alt="Avatar"
width="32"
height="32"
priority
(click)="logout()"
/>
} @else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class="flex items-center p-menu-item-link"
>
<span [class]="item.icon"></span>
<span class="ml-2">{{ item.label }}</span>
<span class="ml-2">{{ item.label | translate }}</span>
<p-badge *ngIf="item.badge" class="ml-auto" [value]="item.badge" />
<span
*ngIf="item.shortcut"
Expand All @@ -34,5 +34,3 @@
</ng-template>
<ng-template #end></ng-template>
</p-menu>

<p-menu [model]="settings" class="*:border-none" />
30 changes: 13 additions & 17 deletions sep490-frontend/src/app/components/sidebar/sidebar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
UserData
} from '../../modules/core/services/application.service';
import {SubscriptionAwareComponent} from '../../modules/core/subscription-aware.component';
import {TranslateService} from '@ngx-translate/core';

@Component({
selector: 'app-sidebar',
Expand All @@ -22,12 +21,9 @@ export class SidebarComponent
{
items: MenuItem[] | undefined;

settings: MenuItem[] | undefined;

constructor(
private readonly applicationService: ApplicationService,
private readonly router: Router,
private readonly translate: TranslateService
private readonly router: Router
) {
super();
}
Expand All @@ -52,9 +48,9 @@ export class SidebarComponent
}
}
);
this.settings = [
{label: 'Settings', icon: 'pi pi-cog', route: '/settings'}
];
// this.settings = [
// {label: 'Settings', icon: 'pi pi-cog', route: '/settings'}
// ];
}

homepage(): void {
Expand All @@ -68,37 +64,37 @@ export class SidebarComponent
label: 'enterprise.title',
items: [
{
label: 'Dashboard',
label: 'sidebar.owner.dashboard',
icon: 'pi pi-chart-line',
route: `/${AppRoutingConstants.DASHBOARD_PATH}`
},
{
label: 'Coefficient center',
label: 'sidebar.owner.coefficientCenter',
icon: 'pi pi-percentage',
route: `/${AppRoutingConstants.EMISSIONS_PATH}`
},
{
label: 'Building',
label: 'sidebar.owner.building',
icon: 'pi pi-building',
route: `/${AppRoutingConstants.ENTERPRISE_PATH}/${AppRoutingConstants.BUILDING_PATH}`
}
]
},
{
label: 'Manage',
label: 'sidebar.owner.manage',
items: [
{
label: 'Users',
label: 'sidebar.owner.users',
icon: 'pi pi-users',
route: `/${AppRoutingConstants.AUTH_PATH}/${AppRoutingConstants.USERS_PATH}`
},
{
label: 'Subscription',
label: 'sidebar.owner.subscription',
icon: 'pi pi-money-bill',
route: `/${AppRoutingConstants.ENTERPRISE_PATH}/${AppRoutingConstants.PLAN_PATH}`
},
{
label: 'Payment',
label: 'sidebar.owner.payment',
icon: 'pi pi-wallet',
route: `/${AppRoutingConstants.ENTERPRISE_PATH}/${AppRoutingConstants.PAYMENT_PATH}`
}
Expand All @@ -110,10 +106,10 @@ export class SidebarComponent
private buildAdminMenu(): MenuItem[] {
return [
{
label: 'Admin',
label: 'sidebar.admin.title',
items: [
{
label: 'Package Credit',
label: 'sidebar.admin.packageCredit',
icon: 'pi pi-chart-line',
route: `/${AppRoutingConstants.ADMIN_PATH}/${AppRoutingConstants.PACKAGE_CREDIT_PATH}`
}
Expand Down
Loading

0 comments on commit 85ae143

Please sign in to comment.