Skip to content

Instantly share code, notes, and snippets.

View SalithaUCSC's full-sized avatar
🚩
Enjoying Life

Salitha Chathuranga SalithaUCSC

🚩
Enjoying Life
View GitHub Profile
@SalithaUCSC
SalithaUCSC / ProductServiceImpl.java
Created January 20, 2021 02:40
Product Service Implementation for REST API
package com.api.reporting.services;
import java.util.List;
import java.util.Optional;
import com.api.reporting.exceptions.ProductNotFoundException;
import com.api.reporting.models.Product;
import com.api.reporting.respositories.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@SalithaUCSC
SalithaUCSC / ProductService.java
Created January 20, 2021 02:39
Product Service interface for REST API
public interface ProductService {
Product getProductById(long productId);
}
@SalithaUCSC
SalithaUCSC / ProductController.java
Created January 20, 2021 02:38
Product Contoller for REST API
package com.api.reporting.controllers;
import com.api.reporting.models.Product;
import com.api.reporting.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@SalithaUCSC
SalithaUCSC / ProductRepositoryImpl.java
Created January 10, 2021 12:39
Custom repository implementation for ProductRepositoryCustom
package com.api.reporting.respositories;
import com.api.reporting.models.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.List;
import java.util.Optional;
@SalithaUCSC
SalithaUCSC / ProductRepositoryCustom.java
Created January 10, 2021 12:35
Custom repository created for ProductRepository
import com.api.reporting.models.Product;
import java.util.List;
import java.util.Optional;
public interface ProductRepositoryCustom {
Optional<List<Product>> findByName(String name);
}
@SalithaUCSC
SalithaUCSC / Product.java
Created January 10, 2021 12:17
Pojo class created for a simple product item
package com.api.reporting.models;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "products")
public class Product {
@Id
private long id;
@SalithaUCSC
SalithaUCSC / ProductRepository.java
Created January 10, 2021 12:04
Traditional repository implementation for MongoDB - Pojo = Product
import com.api.reporting.models.Product;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface ProductRepository extends MongoRepository<Product, Long> {
}
@SalithaUCSC
SalithaUCSC / ProductRepository.java
Created December 30, 2020 12:52
Mongo Data Repository Interface for Pojo - Product
import com.api.reporting.models.Product;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface ProductRepository extends MongoRepository<Product, Long>, ProductRepositoryCustom {
}
@SalithaUCSC
SalithaUCSC / LambdaDemo.java
Last active April 8, 2020 13:07
Sample programs to understand Java Lambda Expressions
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class LambdaDemo {
public static void main(String[] args) {
// CREATE SEPARATE CONSUMER TO MAKE STRINGS UPPERCASE
List<String> names = Arrays.asList("Sam", "Bob", "Sim");
// APPLY LAMBDA
@SalithaUCSC
SalithaUCSC / StreamExamples.java
Last active April 8, 2020 11:13
Sample programs to practice Java Streams
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
class Person {
private String name;
private int age;