Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firat attempt at adding some business logic.. #8

Merged
merged 4 commits into from Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,5 @@
package com.g2.studentservice.api.mock;

public class MockUrlPaths {
public static final String EPOKMODUL = "/modul";
}
@@ -0,0 +1,11 @@
package com.g2.studentservice.api.mock.epok;

import com.g2.studentservice.api.mock.MockUrlPaths;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public interface EpokModulResource {
@RequestMapping(method = RequestMethod.GET, path = MockUrlPaths.EPOKMODUL)
ResponseEntity<ModulResponse> getModul();
}
@@ -0,0 +1,18 @@
package com.g2.studentservice.api.mock.epok;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
@NoArgsConstructor
public class ModulResponse {
private String code;
private String description;
private ModulStatus status;
}
@@ -0,0 +1,8 @@
package com.g2.studentservice.api.mock.epok;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

public enum ModulStatus {
aktiv, inaktiv_framtida, inaktiv_avslutad
}
@@ -0,0 +1,15 @@
package com.g2.studentservice.api.mock.studentits;

import com.g2.studentservice.api.mock.MockUrlPaths;
import com.g2.studentservice.api.mock.epok.ModulResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

public interface StudentItsResource {

@RequestMapping(method = RequestMethod.GET, path = MockUrlPaths.EPOKMODUL)
ResponseEntity<List<StudentItsResponse>> getAllStudents();
}
@@ -0,0 +1,20 @@
package com.g2.studentservice.api.mock.studentits;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
@NoArgsConstructor
public class StudentItsResponse {
private String ssn;

@JsonAlias("username")
private String studentUser;
}
@@ -0,0 +1,20 @@
package com.g2.studentservice.api.rest;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class CreateStudentRequest {
private String firstname;
private String lastname;
private String ssn;
private String streetAdress;
}
@@ -0,0 +1,14 @@
package com.g2.studentservice.api.rest;

import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@AllArgsConstructor
public class SsnAndStudentUserResponse {
private String ssn;
private String studentUser;
}
@@ -0,0 +1,19 @@
package com.g2.studentservice.api.rest;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class SsnFromStudentUserRequest {
private String studentUser;
}
Expand Up @@ -2,10 +2,17 @@

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public interface StudentResource {
@RequestMapping(method = RequestMethod.GET, path = UrlPaths.STUDENT_RESOURCE)
ResponseEntity<StudentResponse> getStudent(@PathVariable long studentId);
@RequestMapping(method = RequestMethod.GET, path = UrlPaths.STUDENT_GET)
ResponseEntity<StudentResponse> getStudent(@PathVariable("studentId") long studentId);

@RequestMapping(method = RequestMethod.POST, path = UrlPaths.SSN_FROM_STUDENTUSER)
ResponseEntity<SsnAndStudentUserResponse> getSsnFromStudentUser(@RequestBody SsnFromStudentUserRequest request);

@RequestMapping(method = RequestMethod.POST, path = UrlPaths.STUDENT_CREATE)
ResponseEntity<StudentResponse> createStudent(@RequestBody CreateStudentRequest request);
}
Expand Up @@ -14,7 +14,8 @@
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class StudentResponse {

private String studentId;
private long studentId;
private String studentUser;
private String firstName;
private String email;
private String ssn;
Expand Down
Expand Up @@ -3,5 +3,9 @@
public class UrlPaths {
public static final String BASE_URI = "/student-service/";
public static final String V1 = "V1/";
public static final String STUDENT_RESOURCE = BASE_URI + V1 + "student/{studentId}";
public static final String STUDENT_GET = BASE_URI + V1 + "student/{studentId}";
public static final String STUDENT_CREATE = BASE_URI + V1 + "student/create";

public static final String SSN_FROM_STUDENTUSER = BASE_URI + V1 + "util/ssn";
public static final String STUDENTUSER_FROM_SSN = BASE_URI + V1 + "util/student-user";
}
2 changes: 2 additions & 0 deletions service/build.gradle
Expand Up @@ -9,10 +9,12 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably drop these dependencies to JPA and h2 database.

implementation 'org.springdoc:springdoc-openapi-ui:1.2.32'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
runtime 'com.h2database:h2'
api project(':api')

testImplementation('org.springframework.boot:spring-boot-starter-test') {
Expand Down
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients("com.g2.studentservice.infrastructure")
@SpringBootApplication
public class StudentServiceApplication {

Expand Down
@@ -0,0 +1,16 @@
package com.g2.studentservice.application;

import com.g2.studentservice.api.mock.studentits.StudentItsResponse;
import com.g2.studentservice.api.rest.CreateStudentRequest;
import com.g2.studentservice.domain.StudentEntity;

import java.util.List;

public interface StudentService {
StudentEntity findStudentById(long studentId);
StudentEntity create(CreateStudentRequest student);

List<StudentItsResponse> getAllStudents();

StudentItsResponse getStudentItsSsn(String studentUser);
}
@@ -0,0 +1,66 @@
package com.g2.studentservice.application.impl;

import com.g2.studentservice.api.mock.studentits.StudentItsResponse;
import com.g2.studentservice.api.rest.CreateStudentRequest;
import com.g2.studentservice.application.StudentService;
import com.g2.studentservice.domain.StudentEntity;
import com.g2.studentservice.infrastructure.StudentRepository;
import com.g2.studentservice.infrastructure.rest.StudentItsClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@RequiredArgsConstructor
@Service
@Slf4j
public class StudentServiceImpl implements StudentService {

@Autowired
private final StudentRepository repository;


private final StudentItsClient client;

@Override
public StudentEntity findStudentById(long studentId) {
return repository.findById(studentId).get();
}

@Override
public StudentEntity create(CreateStudentRequest request){
String studentUser = studentUserGenerator(request.getFirstname(), request.getLastname());
val student = StudentEntity.builder()
.studentUser(studentUser)
.emailAddress(studentUser+"@fake.ltu.se")
.firstname(request.getFirstname())
.lastname(request.getLastname())
.ssn(request.getSsn())
.streetAddress(request.getStreetAdress()).build();
return repository.save(student);
}

private String studentUserGenerator(String firstname, String lastname){
Random random = new Random();
int number = random.nextInt(200 - 2)+2;
return firstname.substring(0,2)+ lastname.substring(0,3)+"-"+number;
}

@Override
public List<StudentItsResponse> getAllStudents(){
val students = client.getAllStudents();
return students;
}

@Override
public StudentItsResponse getStudentItsSsn(String studentUser){
val students = client.getAllStudents();
students.forEach(it -> {log.warn("studentuser "+it.getStudentUser()+" person "+it.getSsn());});
val student = students.stream().filter(it -> it.getStudentUser().equals(studentUser)).findFirst();
return student.get();
}
}
@@ -0,0 +1,4 @@
package com.g2.studentservice.conf;

public class FeignConfig {
}
@@ -0,0 +1,37 @@
package com.g2.studentservice.domain;

import lombok.Builder;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.Instant;

@Data
@Builder
@Entity
public class StudentEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long studentId;

@Column(unique = true)
String studentUser;

@NotNull
String firstname;

@NotNull
String lastname;

@NotNull
String ssn;

String emailAddress;
String streetAddress;

@CreatedDate
Instant createdAt;

}
@@ -0,0 +1,13 @@
package com.g2.studentservice.infrastructure;

import com.g2.studentservice.domain.StudentEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface StudentRepository extends CrudRepository<StudentEntity, Long> {

}

@@ -0,0 +1,23 @@
package com.g2.studentservice.infrastructure;

import com.g2.studentservice.domain.StudentEntity;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Optional;

public class StudentRepositoryImpl{

public Optional<StudentEntity> findById(long studentId) {
return Optional.of(StudentEntity.builder()
.studentId(studentId)
.emailAddress(studentId + "@fakestudent@ltu.se")
.firstname("Miranda")
.lastname("Titania")
.streetAddress("Uranusvägen 3")
.ssn("101010A2345")
.createdAt(Instant.now().minus(10, ChronoUnit.DAYS)).build());
}


}
@@ -0,0 +1,13 @@
package com.g2.studentservice.infrastructure.rest;

import com.g2.studentservice.api.mock.studentits.StudentItsResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@FeignClient(name = "studentits-client", url = "${integration.services.mock-service.url}")
public interface StudentItsClient {
@GetMapping("/students")
List<StudentItsResponse> getAllStudents();
}