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

Task2 #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package au.com.telstra.controller;

import au.com.telstra.model.SIMCard;
import au.com.telstra.pojo.SimCardResponseDTO;
import au.com.telstra.service.SimCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
public class SimCardActivateController {

private static final String ACTIVATE_SIM_API = "http://localhost:8444/actuate";

RestTemplate restTemplate = new RestTemplate();
@Autowired
private SimCardService simCardService;

@PostMapping("/activatesimcard")
public void addUser(@RequestBody SIMCard newSimCard) {
System.out.println(callActivateSimCard(newSimCard));
simCardService.addSimCard(newSimCard);
}

public String callActivateSimCard(SIMCard newSimCard) {
ResponseEntity<Object> response = restTemplate.postForEntity(ACTIVATE_SIM_API, newSimCard, Object.class);
return response.getBody().toString();
}

@GetMapping("/{simCardId}")
public SimCardResponseDTO getSimCardByID(@PathVariable long simCardId) {
SIMCard sim = simCardService.getSimCardById(simCardId);
return new SimCardResponseDTO(sim.getIccid(),
sim.getCustomerEmail(),
true
);
}


}
22 changes: 22 additions & 0 deletions src/main/java/au/com/telstra/model/SIMCard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package au.com.telstra.model;

public class SIMCard {
private String iccid;
private String customerEmail;

public String getIccid() {
return iccid;
}

public void setIccid(String iccid) {
this.iccid = iccid;
}

public String getCustomerEmail() {
return customerEmail;
}

public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
}
31 changes: 31 additions & 0 deletions src/main/java/au/com/telstra/pojo/SimCardEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package au.com.telstra.pojo;

import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class SimCardEntity {
public SimCardEntity(String iccid, String customerEmail, boolean active) {
this.iccid = iccid;
this.customerEmail = customerEmail;
this.active = active;
}

@Id
@GeneratedValue
private long id;
private String iccid;
private String customerEmail;
private boolean active;

public void setId(Long id) {
this.id = id;
}

public Long getId() {
return id;
}
}
37 changes: 37 additions & 0 deletions src/main/java/au/com/telstra/pojo/SimCardResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package au.com.telstra.pojo;

public class SimCardResponseDTO {
public SimCardResponseDTO(String iccid, String customerEmail, boolean active) {
this.iccid = iccid;
this.customerEmail = customerEmail;
this.active = active;
}

private String iccid;
private String customerEmail;
private boolean active;

public String getIccid() {
return iccid;
}

public void setIccid(String iccid) {
this.iccid = iccid;
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

public String getCustomerEmail() {
return customerEmail;
}

public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
}
16 changes: 16 additions & 0 deletions src/main/java/au/com/telstra/repository/SimRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package au.com.telstra.repository;

import au.com.telstra.model.SIMCard;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface SimRepository extends JpaRepository<SIMCard,Long> {
@Override
<S extends SIMCard> S save(S entity);

@Override
Optional<SIMCard> findById(Long aLong);
}
23 changes: 23 additions & 0 deletions src/main/java/au/com/telstra/service/SimCardService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package au.com.telstra.service;

import au.com.telstra.model.SIMCard;
import au.com.telstra.repository.SimRepository;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SimCardService {
private SimRepository simRepository;

public SIMCard addSimCard(SIMCard simCard){
return simRepository.save(simCard);
}

public SIMCard getSimCardById(long simCardId){
return simRepository.findById(simCardId).isPresent()
? simRepository.findById(simCardId).get()
: null;
}

}