Skip to content

Commit

Permalink
Case 2: XMLParser was added for daily Parity data and called when the…
Browse files Browse the repository at this point in the history
… user GET request was sent.

type : feat

- Daily Parity data in XML format taken from https://www.tcmb.gov.tr/kurlar/today.xml site was parsed.
- XMLParser Class is defined to parse the website and return a parity list.
-When the user makes a GET request, XMLParser runs and it is called within the method that the request made.
  • Loading branch information
muhammedalikocabey committed Dec 4, 2020
1 parent f213a64 commit cdd2cd5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
26 changes: 24 additions & 2 deletions src/main/java/com/makocabey/rest/ParityController.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ class ParityController {
@Autowired
private final CalculateNonExistParity calculateParity;


@Autowired
private final XMLService xmlService;

ParityController(ParityRepository repository,
CalculateNonExistParity calculateParity) {
CalculateNonExistParity calculateParity,
XMLService xmlService) {

this.repository = repository;
this.calculateParity = calculateParity;
this.xmlService = xmlService;
}


Expand Down Expand Up @@ -331,4 +334,23 @@ public List<CustomParityResponse> calculateMethodByParityCodeBetweenDates(

return listOfResponse;
}





@GetMapping("/dailyparity")
public ResponseEntity<String> getDailyParityData() {
List<Parity> listOfParity = xmlService.parseAndSaveParityData();

LocalDate todaysDate = listOfParity.get(0).getDate();

if(!((repository.findByDate(todaysDate)).isEmpty())) {
repository.deleteByDate(todaysDate);
}

repository.saveAll(listOfParity);

return new ResponseEntity<String>("The parity data of day{" + todaysDate + "} has been successfully saved to the database.", HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@


@SpringBootApplication
public class SpringRestServiceApplication implements CommandLineRunner {

private XMLService xmlService;

public SpringRestServiceApplication(XMLService xmlService) {
this.xmlService = xmlService;
}

public class SpringRestServiceApplication {

public static void main(String[] args) {

Expand All @@ -25,11 +18,4 @@ public static void main(String[] args) {
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}



@Override
public void run(String...args) throws Exception {
xmlService.parseAndSaveParityData();
}
}
22 changes: 9 additions & 13 deletions src/main/java/com/makocabey/rest/XMLService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.makocabey.rest;


import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand All @@ -10,6 +10,8 @@

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -19,22 +21,16 @@



@Service
@Component
public class XMLService {

private final Logger log = LoggerFactory.getLogger(XMLService.class);


private final ParityRepository repository;

public XMLService(ParityRepository repository) {
this.repository = repository;
}



public void parseAndSaveParityData() {
public List<Parity> parseAndSaveParityData() {

List<Parity> listOfParity = new ArrayList<>();

try {
String URL = "https://www.tcmb.gov.tr/kurlar/today.xml";
Expand Down Expand Up @@ -77,9 +73,7 @@ public void parseAndSaveParityData() {

Parity parity = new Parity(parityCode, date, purchasePrice, salePrice, averagePrice);


repository.save(parity);

listOfParity.add(parity);
}

}
Expand All @@ -90,6 +84,8 @@ public void parseAndSaveParityData() {
catch (Exception e) {
log.error(e.getMessage());
}

return listOfParity;
}

}

0 comments on commit cdd2cd5

Please sign in to comment.