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;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import javax.persistence.Entity;
@SpringBootApplication
@EntityScan("edu.prlab.tyler.iotgateway")
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
}
\ No newline at end of file
......@@ -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.privacy.PrivacyChoice;
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.services.DeviceService;
import edu.prlab.tyler.iotgateway.gateway.services.PrivacyService;
......@@ -56,7 +57,7 @@ public class GatewayController {
//設定隱私選擇
@PostMapping("/choice")
public ResponseEntity<PrivacyChoice> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) {
public ResponseEntity<PrivacyChoiceResponse> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) {
return privacyService.setPrivacyPolicyChoice(privacyChoice)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build());
......@@ -66,7 +67,7 @@ public class GatewayController {
//根據使用者取得在此gateway上該使用者的隱私選擇列表(根據時間排序),
//暫時從DB撈,無特定使用者
@GetMapping("/choice")
public ResponseEntity<Iterable<PrivacyChoiceIndex>> readPrivacyChoiceRecordsByUser() {
public ResponseEntity<Iterable<PrivacyChoiceResponse>> readPrivacyChoiceRecordsByUser() {
return Optional.of(privacyService.getPrivacyPolicyChoices())
.map(ResponseEntity::ok)
.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;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
......
......@@ -5,5 +5,5 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.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;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
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.repositories.PrivacyChoiceIndexRepository;
import org.springframework.beans.factory.annotation.Autowired;
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.web.client.RestTemplate;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.stream.StreamSupport;
@Service
public class PrivacyService {
......@@ -35,14 +39,18 @@ public class PrivacyService {
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)
.map(choice -> template.postForObject(cloudAddress + "/choice", choice, PrivacyChoice.class))
.map(choice -> privacyChoiceIndexRepository.save(PrivacyChoiceIndex.builder()
.id(choice.getId())
.localDateTime(LocalDateTime.now())
.build()))
.map(choice -> privacyChoice);
.map(choice -> PrivacyChoiceResponse.builder()
.id(choice.getId())
.localDateTime(LocalDateTime.now())
.privacyContent(privacyChoice.getPrivacyContent())
.build());
}
// public Optional<Iterable<PrivacyChoice>> getPrivacyPolicyChoices() {
......@@ -50,7 +58,19 @@ public class PrivacyService {
// new ParameterizedTypeReference<Iterable<PrivacyChoice>>() { }).getBody());
// }
public Iterable<PrivacyChoiceIndex> getPrivacyPolicyChoices() {
return privacyChoiceIndexRepository.findAll();
public Iterable<PrivacyChoiceResponse> getPrivacyPolicyChoices() {
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 {
.andDo(print())
.andExpect(status().isOk())
.andReturn();
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