Home page
This section is part of a the tutorial series to build a Product Highlights example Extension.
What you’ll learn
In this section, you'll use React, MongoDB, and Nitrozen components to build your Extension home page.
This change consists of three steps:
- Create schema for saving product and its highlights using mongodb.
- Make changes in the Extension backend.
- Add additional components to the Home page of the Extension Frontend.
Step 1: Create MongoDB config
The app needs a database to store the Product details so that merchants can view and edit the saved product highlights. For this we will require mongo connection.
-
Add the following dependency inside
./pom.xml
file<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.7.5</version>
</dependency> -
Create a new file
/com/fynd/example/java/db/MongoConfig.java
and add the following code:package com.fynd.example.java.db;
import com.fynd.example.java.properties.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
@Configuration
public class MongoConfig {
@Autowired
Config config;
@Bean
public MongoTemplate mongoTemplate() {
String mongodbUri = config.getMongodbUri();
return new MongoTemplate(new SimpleMongoClientDatabaseFactory(mongodbUri));
}
}
Step 2: Create MongoDB schema
The database collection includes the product highlights, as well as the basic details about the product such as name, product brand, product slug, and product item code.
-
Create new
/com/fynd/example/java/db/Product.java
classpackage com.fynd.example.java.db;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fynd.example.java.helper.models.Price;
import lombok.*;
import java.util.List;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Product {
@JsonProperty("name")
private String name;
@JsonProperty("image")
private String image;
@JsonProperty("brand_name")
private String branchName;
@JsonProperty("category_slug")
private String categorySlug;
@JsonProperty("highlights")
private List<String> highlights;
@JsonProperty("price")
private Price price;
@JsonProperty("enablePriceDrop")
private Boolean enablePriceDrop;
} -
Also crate
/com/fynd/example/java/db/ProductHighlight.java
classpackage com.fynd.example.java.db;
import lombok.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "product-highlights")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ProductHighlight {
@JsonProperty("company_id")
private String companyId;
@JsonProperty("application_id")
private String applicationId;
@JsonProperty("product_item_code")
private Integer productItemCode;
@JsonProperty("product_slug")
private String productSlug;
@JsonProperty("product")
private Product product;
@JsonProperty(value = "is_active", defaultValue = "false")
private Boolean isActive;
} -
Create new
/com/fynd/example/java/db/interfaces/ProductHighlightRepository.java
file and add the following code to the filepackage com.fynd.example.java.db.interfaces;
import com.fynd.example.java.db.ProductHighlight;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface ProductHighlightRepository extends MongoRepository<ProductHighlight, String> {
List<ProductHighlight> findByCompanyIdAndIsActive(String companyId);
List<ProductHighlight> findByCompanyIdAndApplicationId(String companyId, String applicationId);
Optional<ProductHighlight> findOneByCompanyIdAndApplicationIdAndProductItemCode(String companyId, String applicationId, Integer productItemCode);
Optional<ProductHighlight> findOneByCompanyIdAndApplicationIdAndProductSlug(String companyId, String applicationId, String productSlug);
Optional<ProductHighlight> findOneByCompanyIdAndProductItemCode(String companyId, Integer ProductItemCode);
Optional<ProductHighlight> findOneByApplicationIdAndProductSlug(String applicationId, String productSlug);
void deleteOneByCompanyIdAndApplicationIdAndProductItemCode(String companyId, String applicationId, Integer productItemCode);
void deleteOneByCompanyIdAndApplicationIdAndProductSlug(String companyId, String applicationId, String productSlug);
long countByCompanyIdAndApplicationIdAndIsActive(String companyId, String applicationId, Boolean isActive);
}