Commit 328e0c1e authored by 0Tyler's avatar 0Tyler

PrivacyChoice api

parent 31e75c68
This diff is collapsed.
...@@ -2,10 +2,14 @@ package edu.prlab.tyler.iotgateway.gateway; ...@@ -2,10 +2,14 @@ package edu.prlab.tyler.iotgateway.gateway;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import javax.persistence.Entity;
@SpringBootApplication @SpringBootApplication
@EntityScan("edu.prlab.tyler.iotgateway")
public class GatewayApplication { public class GatewayApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args); SpringApplication.run(GatewayApplication.class, args);
} }
} }
\ No newline at end of file
...@@ -3,6 +3,7 @@ package edu.prlab.tyler.iotgateway.gateway.controllers; ...@@ -3,6 +3,7 @@ package edu.prlab.tyler.iotgateway.gateway.controllers;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.gateway.model.PrivacyChoiceResponse;
import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex; import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex;
import edu.prlab.tyler.iotgateway.gateway.services.DeviceService; import edu.prlab.tyler.iotgateway.gateway.services.DeviceService;
import edu.prlab.tyler.iotgateway.gateway.services.PrivacyService; import edu.prlab.tyler.iotgateway.gateway.services.PrivacyService;
...@@ -56,7 +57,7 @@ public class GatewayController { ...@@ -56,7 +57,7 @@ public class GatewayController {
//設定隱私選擇 //設定隱私選擇
@PostMapping("/choice") @PostMapping("/choice")
public ResponseEntity<PrivacyChoice> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) { public ResponseEntity<PrivacyChoiceResponse> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) {
return privacyService.setPrivacyPolicyChoice(privacyChoice) return privacyService.setPrivacyPolicyChoice(privacyChoice)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build()); .orElseGet(() -> ResponseEntity.noContent().build());
...@@ -66,7 +67,7 @@ public class GatewayController { ...@@ -66,7 +67,7 @@ public class GatewayController {
//根據使用者取得在此gateway上該使用者的隱私選擇列表(根據時間排序), //根據使用者取得在此gateway上該使用者的隱私選擇列表(根據時間排序),
//暫時從DB撈,無特定使用者 //暫時從DB撈,無特定使用者
@GetMapping("/choice") @GetMapping("/choice")
public ResponseEntity<Iterable<PrivacyChoiceIndex>> readPrivacyChoiceRecordsByUser() { public ResponseEntity<Iterable<PrivacyChoiceResponse>> readPrivacyChoiceRecordsByUser() {
return Optional.of(privacyService.getPrivacyPolicyChoices()) return Optional.of(privacyService.getPrivacyPolicyChoices())
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build()); .orElseGet(() -> ResponseEntity.noContent().build());
......
package edu.prlab.tyler.iotgateway.gateway.model;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PrivacyChoiceResponse {
private long id;
private LocalDateTime localDateTime;
private PrivacyContent privacyContent;
}
package edu.prlab.tyler.iotgateway.gateway.pojo; package edu.prlab.tyler.iotgateway.gateway.pojo;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
......
...@@ -5,5 +5,5 @@ import org.springframework.data.repository.CrudRepository; ...@@ -5,5 +5,5 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface PrivacyChoiceIndexRepository extends CrudRepository<PrivacyChoiceIndex, String> { public interface PrivacyChoiceIndexRepository extends CrudRepository<PrivacyChoiceIndex, Long> {
} }
...@@ -2,15 +2,19 @@ package edu.prlab.tyler.iotgateway.gateway.services; ...@@ -2,15 +2,19 @@ package edu.prlab.tyler.iotgateway.gateway.services;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.gateway.model.PrivacyChoiceResponse;
import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex; import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex;
import edu.prlab.tyler.iotgateway.gateway.repositories.PrivacyChoiceIndexRepository; import edu.prlab.tyler.iotgateway.gateway.repositories.PrivacyChoiceIndexRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Optional; import java.util.Optional;
import java.util.stream.StreamSupport;
@Service @Service
public class PrivacyService { public class PrivacyService {
...@@ -35,14 +39,18 @@ public class PrivacyService { ...@@ -35,14 +39,18 @@ public class PrivacyService {
return Optional.ofNullable(template.getForObject(cloudAddress + "/privacy/" + udn, PrivacyPolicyReport.class)); return Optional.ofNullable(template.getForObject(cloudAddress + "/privacy/" + udn, PrivacyPolicyReport.class));
} }
public Optional<PrivacyChoice> setPrivacyPolicyChoice(PrivacyChoice privacyChoice) { public Optional<PrivacyChoiceResponse> setPrivacyPolicyChoice(PrivacyChoice privacyChoice) {
return Optional.of(privacyChoice) return Optional.of(privacyChoice)
.map(choice -> template.postForObject(cloudAddress + "/choice", choice, PrivacyChoice.class)) .map(choice -> template.postForObject(cloudAddress + "/choice", choice, PrivacyChoice.class))
.map(choice -> privacyChoiceIndexRepository.save(PrivacyChoiceIndex.builder() .map(choice -> privacyChoiceIndexRepository.save(PrivacyChoiceIndex.builder()
.id(choice.getId()) .id(choice.getId())
.localDateTime(LocalDateTime.now()) .localDateTime(LocalDateTime.now())
.build())) .build()))
.map(choice -> privacyChoice); .map(choice -> PrivacyChoiceResponse.builder()
.id(choice.getId())
.localDateTime(LocalDateTime.now())
.privacyContent(privacyChoice.getPrivacyContent())
.build());
} }
// public Optional<Iterable<PrivacyChoice>> getPrivacyPolicyChoices() { // public Optional<Iterable<PrivacyChoice>> getPrivacyPolicyChoices() {
...@@ -50,7 +58,19 @@ public class PrivacyService { ...@@ -50,7 +58,19 @@ public class PrivacyService {
// new ParameterizedTypeReference<Iterable<PrivacyChoice>>() { }).getBody()); // new ParameterizedTypeReference<Iterable<PrivacyChoice>>() { }).getBody());
// } // }
public Iterable<PrivacyChoiceIndex> getPrivacyPolicyChoices() { public Iterable<PrivacyChoiceResponse> getPrivacyPolicyChoices() {
return privacyChoiceIndexRepository.findAll(); return () -> StreamSupport.stream(template.exchange(cloudAddress + "/choice", HttpMethod.GET, null,
new ParameterizedTypeReference<Iterable<PrivacyChoice>>() {
}).getBody().spliterator(), false)
.map(choice -> privacyChoiceIndexRepository.findById(choice.getId())
.map(choiceIndex -> PrivacyChoiceResponse.builder()
.id(choiceIndex.getId())
.localDateTime(choiceIndex.getLocalDateTime())
.privacyContent(choice.getPrivacyContent())
.build()).get())
.iterator();
} }
} }
...@@ -113,6 +113,7 @@ public class GatewayHttpApiTest { ...@@ -113,6 +113,7 @@ public class GatewayHttpApiTest {
.andDo(print()) .andDo(print())
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
Assert.assertNotNull(result); Assert.assertNotNull(result);
//取得所有隱私偏好 //取得所有隱私偏好
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment