Commit f607ef4d authored by Ashok Kumar K's avatar Ashok Kumar K

Added mongoConfig and basic setup of repo and model classes

parent 8840cd1b
......@@ -24,12 +24,12 @@ ext {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'de.codecentric:spring-boot-admin-starter-client'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
......
#Thu May 21 11:20:32 IST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
......@@ -2,7 +2,9 @@ package com.nisum.ecomcustomer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@EnableMongoRepositories
@SpringBootApplication
public class EcomCustomerApplication {
......
package com.nisum.ecomcustomer.config.mongodb;
import com.nisum.ecomcustomer.model.AddressType;
import com.nisum.ecomcustomer.model.CustomerType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
public class CustomMongoConverters {
@WritingConverter
static class CustomerTypeToIntegerConverter implements Converter<CustomerType, Integer> {
@Override
public Integer convert(CustomerType source) {
return source.getValue();
}
}
@ReadingConverter
static class IntegerToCustomerTypeConverter implements Converter<Integer, CustomerType> {
@Override
public CustomerType convert(Integer source) {
return CustomerType.of(source);
}
}
@WritingConverter
static class AddressTypeToStringConverter implements Converter<AddressType, String> {
@Override
public String convert(AddressType source) {
return source.getValue();
}
}
@ReadingConverter
static class StringToAddressTypeConverter implements Converter<String, AddressType> {
@Override
public AddressType convert(String source) {
return AddressType.of(source);
}
}
}
package com.nisum.ecomcustomer.config.mongodb;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import static com.nisum.ecomcustomer.config.mongodb.CustomMongoConverters.*;
import static com.nisum.ecomcustomer.config.mongodb.CustomMongoConverters.CustomerTypeToIntegerConverter;
import static com.nisum.ecomcustomer.config.mongodb.CustomMongoConverters.IntegerToCustomerTypeConverter;
import static org.springframework.data.mongodb.core.convert.MongoCustomConversions.MongoConverterConfigurationAdapter;
@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
@Value("${spring.data.mongodb.database}")
private String databaseName;
@Override
protected String getDatabaseName() {
return databaseName;
}
//TODO - know about the good practices.
// @Bean
// @Override
// public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory databaseFactory, MongoCustomConversions customConversions, MongoMappingContext mappingContext) {
// MappingMongoConverter mmc = super.mappingMongoConverter(databaseFactory, customConversions, mappingContext);
// mmc.setTypeMapper(getTypeMapper());
// return mmc;
// }
//
// @Bean
// public MongoTypeMapper getTypeMapper() {
// return new CustomMongoTypeMapper();
// }
@Override
protected void configureConverters(MongoConverterConfigurationAdapter adapter) {
adapter.registerConverter(new CustomerTypeToIntegerConverter());
adapter.registerConverter(new IntegerToCustomerTypeConverter());
adapter.registerConverter(new AddressTypeToStringConverter());
adapter.registerConverter(new StringToAddressTypeConverter());
}
}
class CustomMongoTypeMapper extends DefaultMongoTypeMapper {
CustomMongoTypeMapper() {
super(null);
}
}
package com.nisum.ecomcustomer.controller;
import com.nisum.ecomcustomer.model.Customer;
import com.nisum.ecomcustomer.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CustomerController {
@Autowired
CustomerService customerService;
@PostMapping("/")
public Customer register(@RequestBody Customer customer) {
return customerService.register(customer);
}
@GetMapping("/")
public List<Customer> getAllCustomers() {
return customerService.getAllCustomers();
}
}
package com.nisum.ecomcustomer.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Field;
@Data
public class Address {
// @Field("id")
// private Long addressId;
@Field("type")
private AddressType addressType;
@Field("line1")
private String addressLine1;
@Field("line2")
private String addressLine2;
@JsonProperty("isDefault")
private boolean isDefault;
private String city;
private String state;
private String country;
private String zipCode;
}
package com.nisum.ecomcustomer.model;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
public enum AddressType {
SHIPPING("ship"),
BILLING("bill");
@Getter
@JsonValue
private String value;
@Override
public String toString() {
return value;
}
AddressType(String value) {
this.value = value;
}
public static AddressType of(String value) {
switch (value) {
case "ship":
return AddressType.SHIPPING;
case "bill":
return AddressType.BILLING;
default:
throw new IllegalArgumentException(value +" can't be converted to type AddressType");
}
}
}
package com.nisum.ecomcustomer.model;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.util.List;
//TODO - check the relevance of annotations
@NoArgsConstructor
@Data
@TypeAlias("customer")
@Document("customers")
public class Customer {
@Id
private String id;
@Field("fName")
private String firstName;
@Field("lName")
private String lastName;
private String email;
private String dayPhone;
private CustomerType customerType;
List<Address> addresses;
}
package com.nisum.ecomcustomer.model;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
@Getter
public enum CustomerType {
REGULAR(0),
LOYAL(1);
@JsonValue
private int value;
CustomerType(int value) {
this.value = value;
}
@Override
public String toString() {
return Integer.toString(value);
}
public static CustomerType of(int value) {
switch (value) {
case 0:
return CustomerType.REGULAR;
case 1:
return CustomerType.LOYAL;
default:
throw new IllegalArgumentException(value + " can't be converted to type CustomerType");
}
}
}
package com.nisum.ecomcustomer.repository;
import com.nisum.ecomcustomer.model.Customer;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CustomerRepository extends MongoRepository<Customer, String> {
}
package com.nisum.ecomcustomer.service;
import com.nisum.ecomcustomer.model.Customer;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface CustomerService {
public Customer register(Customer customer);
public List<Customer> getAllCustomers();
}
package com.nisum.ecomcustomer.service.impl;
import com.nisum.ecomcustomer.model.Customer;
import com.nisum.ecomcustomer.repository.CustomerRepository;
import com.nisum.ecomcustomer.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
CustomerRepository customerRepository;
@Override
public Customer register(Customer customer) {
return customerRepository.save(customer);
}
@Override
public List<Customer> getAllCustomers() {
return customerRepository.findAll();
}
}
spring:
data:
mongodb:
host: localhost
port: 27017
database: ecom
logging:
level:
web: debug
\ No newline at end of file
server:
port: 8008
spring:
profiles:
active: development
data:
mongodb:
host: localhost
port: 27017
database: ecom
management:
endpoints:
web:
exposure:
include: '*'
spring:
application:
name: customer
\ No newline at end of file
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