Skip to content

Commit

Permalink
[FEAT] 센서 데이터 api 구현
Browse files Browse the repository at this point in the history
* FEAT : 다이나모 기본 설정

* FEAT : gitignore 추가

* FEAT : 센서 데이터 api

* FEAT : build.gradle 수정
  • Loading branch information
gourderased authored Jun 10, 2024
1 parent d8d8b62 commit 9b448bc
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ out/

### VS Code ###
.vscode/
./src/main/resources/application-main.yml
8 changes: 6 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// xml 응답을 위한 의존성
implementation 'org.glassfish.jaxb:jaxb-runtime:3.0.2'


//aws
implementation 'com.amazonaws:aws-java-sdk-dynamodb:1.11.563'

}

tasks.named('test') {
Expand All @@ -50,4 +54,4 @@ tasks.register('copyGitSubmodule', Copy) {
// from './config'
// include 'env.sh'
// into 'scripts'
//}
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.cloudcomputing.ohhanahana.common;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DynamoDbConfig {

@Value("${amazon.aws.access-key}")
private String accessKey;

@Value("${amazon.aws.secret-key}")
private String secretKey;

@Value("${amazon.aws.region}")
private String region;

@Value("${amazon.aws.dynamodb.endpoint}")
private String dynamoDBEndpoint;

@Bean
public DynamoDBMapper dynamoDBMapper() {
DynamoDBMapperConfig mapperConfig = DynamoDBMapperConfig.builder()
.withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.CLOBBER)
.withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT)
.withTableNameOverride(null)

.withPaginationLoadingStrategy(DynamoDBMapperConfig.PaginationLoadingStrategy.EAGER_LOADING)
.build();
DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDB(), mapperConfig);
return mapper;
}
@Bean
public AmazonDynamoDB amazonDynamoDB() {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(new EndpointConfiguration(dynamoDBEndpoint, region))
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
System.out.println(amazonDynamoDB.listTables());
return amazonDynamoDB;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.cloudcomputing.ohhanahana.controller;

import com.cloudcomputing.ohhanahana.entity.Person;
import com.cloudcomputing.ohhanahana.repository.PersonRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
@RequestMapping("/persons")
public class PersonController {

@Autowired
private PersonRepository personRepository;

@PostMapping
public Person save(@RequestBody Person person) {

return personRepository.save(person);
}

@GetMapping("/{id}")
public Person findById(@PathVariable(value = "id") String id) {
return personRepository.findById(id);
}

@GetMapping("/all")
public List<Person> findAll() {
return personRepository.findAll();
}

@PutMapping("/{id}")
public String update(@PathVariable(value="id") String id,
@RequestBody Person person) {
return personRepository.update(id, person);
}

@DeleteMapping("/{id}")
public String delete(@PathVariable(value="id") String id) {
return personRepository.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.cloudcomputing.ohhanahana.controller;

import com.cloudcomputing.ohhanahana.dto.response.SensorDataResponse;
import com.cloudcomputing.ohhanahana.service.SensorDataService;
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.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/congestion")
public class SensorDataController {

private final SensorDataService sensorDataService;

@GetMapping
public ResponseEntity<SensorDataResponse> findSensorData() {
return ResponseEntity.ok(sensorDataService.findSensorData());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cloudcomputing.ohhanahana.dto.response;

import com.cloudcomputing.ohhanahana.enums.Congestion;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder

public class SensorDataResponse {

private String currentLocation;
private String currentDateTime;
private String congestion;
private int expectedWaitingTime;
private int expectedWaitingPeople;

public static SensorDataResponse toDTO(String currentLocation, String currentDateTime,
String congestion,
int expectedWaitingTime, int expectedWaitingPeople) {

return SensorDataResponse.builder()
.currentLocation(currentLocation)
.currentDateTime(currentDateTime)
.congestion(congestion)
.expectedWaitingTime(expectedWaitingTime)
.expectedWaitingPeople(expectedWaitingPeople)
.build();
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/cloudcomputing/ohhanahana/entity/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.cloudcomputing.ohhanahana.entity;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import lombok.Data;

@Data
@DynamoDBTable(tableName = "person")
public class Person {

@DynamoDBHashKey
@DynamoDBAutoGeneratedKey
private String id;

@DynamoDBAttribute
private String firstname;

@DynamoDBAttribute
private String lastname;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.cloudcomputing.ohhanahana.repository;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;
import com.cloudcomputing.ohhanahana.entity.Person;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class PersonRepository {

@Autowired
private DynamoDBMapper dynamoDBMapper;

public Person save(Person person) {
dynamoDBMapper.save(person);
return person;
}

public Person findById(String id) {
return dynamoDBMapper.load(Person.class, id);
}

public List<Person> findAll() {
return dynamoDBMapper.scan(Person.class, new DynamoDBScanExpression());
}

public String update(String id, Person person) {
dynamoDBMapper.save(person,
new DynamoDBSaveExpression()
.withExpectedEntry("id",
new ExpectedAttributeValue(
new AttributeValue().withS(id)
)));
return id;
}

public String delete(String id) {
dynamoDBMapper.delete(id);
return "Person deleted successfully::" +id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cloudcomputing.ohhanahana.repository;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class SensorDataRepository {

public void findLatest() {
//TODO - 다이나모 디비에서 최신값 불러오기
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cloudcomputing.ohhanahana.service;

import com.cloudcomputing.ohhanahana.dto.response.SensorDataResponse;
import com.cloudcomputing.ohhanahana.enums.Congestion;
import com.cloudcomputing.ohhanahana.repository.SensorDataRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SensorDataService {

private final SensorDataRepository sensorDataRepository;

public SensorDataResponse findSensorData() {
sensorDataRepository.findLatest();

String congestion = Congestion.NORMAL.getToKorean();

return SensorDataResponse.toDTO("인하대학교",
"2024-05-31 16:30 PM",
congestion,
25,
20);
}
}
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
application:
name: ohhanahana

0 comments on commit 9b448bc

Please sign in to comment.