Commit fd28b75d authored by sgandhi@nisum.com's avatar sgandhi@nisum.com

[EPE-001] Added a service to get all offers from the database.

parent 32bc37b2
......@@ -16,6 +16,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
......
package com.nisum.epe.demo.controller;
import com.nisum.epe.demo.model.Offer;
import com.nisum.epe.demo.service.OfferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class OfferDemoController {
@Autowired
private OfferService offerService;
@GetMapping("/getOffers")
public List<Offer> getAllOffers(){
return offerService.getAllOffers();
}
}
package com.nisum.epe.demo.dao;
import com.nisum.epe.demo.model.Offer;
import org.springframework.data.repository.CrudRepository;
public interface OfferRepo extends CrudRepository<Offer, String> {
}
package com.nisum.epe.demo.model;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;
@Data
@AllArgsConstructor
@NoArgsConstructor
@FieldDefaults(makeFinal = false,level = AccessLevel.PRIVATE)
public class Offer {
String offerId;
String offerName;
String offerDescription;
Integer usageLimit;
}
package com.nisum.epe.demo.service;
import com.nisum.epe.demo.dao.OfferRepo;
import com.nisum.epe.demo.model.Offer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class OfferService {
@Autowired
private OfferRepo offerRepo;
public List<Offer> getAllOffers() {
List<Offer> offers = new ArrayList<>();
offerRepo.findAll().forEach(t -> offers.add(t));
return offers;
}
}
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