Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushroshan committed Feb 5, 2024
2 parents 791213c + f34fb50 commit 2fdda54
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 112 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ You can use prebuilt images generated by our CI workflow.
docker-compose -f docker-compose.yml --compatibility up -d
```

- To Stop and Cleanup crAPI

```
docker-compose -f docker-compose.yml --compatibility down –volumes
```

Visit [http://localhost:8888](http://localhost:8888)

**Note**: All emails are sent to mailhog service by default and can be checked on
Expand Down
6 changes: 3 additions & 3 deletions deploy/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ services:
resources:
limits:
cpus: '0.3'
memory: 128M
memory: 192M

crapi-workshop:
container_name: crapi-workshop
Expand Down Expand Up @@ -194,8 +194,8 @@ services:
deploy:
resources:
limits:
cpus: '0.3'
memory: 128M
cpus: '0.5'
memory: 256M

mongodb:
container_name: mongodb
Expand Down
2 changes: 1 addition & 1 deletion deploy/vagrant/crapi.service
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Type=oneshot
RemainAfterExit=true
WorkingDirectory=/opt/crapi
ExecStart=/bin/bash -c "/usr/local/bin/docker-compose up -d --remove-orphans"
ExecStop=/bin/bash -c "/usr/local/bin/docker-compose down"
ExecStop=/bin/bash -c "/usr/local/bin/docker-compose down –volumes"

[Install]
WantedBy=multi-user.target
8 changes: 4 additions & 4 deletions openapi-spec/openapi-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@
"schema" : {
"type" : "string",
"format" : "uuid",
"example" : "0be319f0-f0dd-44aa-af0b-af927f3a383f"
"example" : "1929186d-8b67-4163-a208-de52a41f7301"
}
} ]
},
Expand Down Expand Up @@ -2730,8 +2730,8 @@
}
},
"example" : {
"id" : 23,
"vin" : "0FOPP90TFEE927859",
"id" : 3,
"vin" : "1G1OP124017231334",
"owner" : {
"email" : "victim.one@example.com",
"number" : "4156895423"
Expand Down Expand Up @@ -3096,7 +3096,7 @@
"example" : {
"nickname" : "Hacker",
"email" : "hacker@darkweb.com",
"vehicleid" : "abac4018-5a38-466c-ab7f-361908afeab6",
"vehicleid" : "4bae9968-ec7f-4de3-a3a0-ba1b2ab5e5e5",
"profile_pic_url" : "",
"created_at" : "2021-09-16T01:46:32.432Z"
}
Expand Down
122 changes: 103 additions & 19 deletions services/identity/src/main/java/com/crapi/config/InitialDataConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@
package com.crapi.config;

import com.crapi.constant.TestUsers;
import com.crapi.entity.ProfileVideo;
import com.crapi.entity.User;
import com.crapi.entity.UserDetails;
import com.crapi.entity.VehicleCompany;
import com.crapi.entity.VehicleDetails;
import com.crapi.entity.VehicleLocation;
import com.crapi.entity.VehicleModel;
import com.crapi.enums.EFuelType;
import com.crapi.enums.ERole;
import com.crapi.model.SeedUser;
import com.crapi.repository.*;
import com.crapi.service.VehicleService;
import com.crapi.utils.GenerateVIN;
import com.crapi.utils.UserData;
import com.crapi.utils.VehicleLocationData;
import com.crapi.utils.VehicleModelData;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -40,8 +46,16 @@ public class InitialDataConfig {

private static final Logger logger = LoggerFactory.getLogger(InitialDataConfig.class);

private static long seed = 0;

private Random random;

GenerateVIN generateVIN;

@Autowired VehicleLocationRepository vehicleLocationRepository;

@Autowired VehicleCompanyRepository vehicleCompanyRepository;

@Autowired VehicleModelRepository vehicleModelRepository;

@Autowired VehicleDetailsRepository vehicleDetailsRepository;
Expand All @@ -56,30 +70,57 @@ public class InitialDataConfig {

@Autowired PasswordEncoder encoder;

public void addLocation() {
if (CollectionUtils.isEmpty(vehicleLocationRepository.findAll())) {
VehicleLocationData vehicleLocationData = new VehicleLocationData();
vehicleLocationRepository.saveAll(vehicleLocationData.getVehicleLocationData());
}
public void createModels() {
VehicleCompany vehicleCompany = new VehicleCompany("Hyundai");
VehicleModel vehicleModel =
new VehicleModel("Creta", EFuelType.DIESEL, vehicleCompany, "images/hyundai-creta.jpg");
vehicleModelRepository.save(vehicleModel);

vehicleCompany = new VehicleCompany("Lamborghini");
vehicleModel =
new VehicleModel(
"Aventador", EFuelType.PETROL, vehicleCompany, "images/lamborghini-aventador.jpg");
vehicleModel = vehicleModelRepository.save(vehicleModel);

vehicleCompany = new VehicleCompany("Mercedes-Benz");
vehicleModel =
new VehicleModel(
"GLA Class", EFuelType.DIESEL, vehicleCompany, "images/mercedesbenz-gla.jpg");
vehicleModelRepository.save(vehicleModel);

vehicleCompany = new VehicleCompany("BMW");
vehicleModel =
new VehicleModel("5 Series", EFuelType.PETROL, vehicleCompany, "images/bmw-5.jpg");
vehicleModelRepository.save(vehicleModel);

vehicleCompany = new VehicleCompany("Audi");
vehicleModel = new VehicleModel("RS7", EFuelType.DIESEL, vehicleCompany, "images/audi-rs7.jpg");
vehicleModelRepository.save(vehicleModel);

vehicleCompany = new VehicleCompany("MG Motor");
vehicleModel =
new VehicleModel(
"Hector Plus", EFuelType.PETROL, vehicleCompany, "images/mgmotor-hectorplus.jpg");
vehicleModel = vehicleModelRepository.save(vehicleModel);
}

public void addVehicleModel() {
if (CollectionUtils.isEmpty(vehicleModelRepository.findAll())) {
VehicleModelData vehicleModelData = new VehicleModelData();
vehicleModelRepository.saveAll(vehicleModelData.getModelList());
createModels();
}
}

@EventListener
public void setup(ApplicationReadyEvent event) {

addLocation();
random = new Random();
random.setSeed(seed);
generateVIN = new GenerateVIN();
addVehicleModel();
addUser();
}

public void addUser() {
if (CollectionUtils.isEmpty(userDetailsRepository.findAll())) {
if (CollectionUtils.isEmpty(userDetailsRepository.findAll()) || false) {
ArrayList<SeedUser> userDetailList = new TestUsers().getUsers();
for (SeedUser userDetails : userDetailList) {
boolean user =
Expand All @@ -88,32 +129,75 @@ public void addUser() {
userDetails.getEmail(),
userDetails.getPassword(),
userDetails.getNumber(),
userDetails.getRole());
userDetails.getRole(),
userDetails.getCarid(),
userDetails.getVin(),
userDetails.getPincode(),
userDetails.getLatitude(),
userDetails.getLongitude());
if (!user) {
logger.error("Fail to create predefined users");
}
}
}
}

public VehicleDetails createVehicle(
String carId, String vin, String pincode, String latitude, String longitude) {
List<VehicleModel> modelList = null;
modelList = vehicleModelRepository.findAll();
if (modelList != null) {
VehicleLocation vehicleLocation = new VehicleLocation(latitude, longitude);
VehicleDetails vehicleDetails = new VehicleDetails(carId, pincode, vin);
VehicleModel vehicleModel = modelList.get(random.nextInt(modelList.size()));
vehicleModel = vehicleModelRepository.findById(vehicleModel.getId()).get();
vehicleDetails.setVehicleLocation(vehicleLocation);
vehicleDetails = vehicleDetailsRepository.save(vehicleDetails);
vehicleDetails.setModel(vehicleModel);
vehicleDetails = vehicleDetailsRepository.save(vehicleDetails);
logger.debug("Created vehicle for {} successfully", vehicleDetails);
return vehicleDetails;
}
return null;
}

public boolean predefineUserData(
String name, String email, String password, String number, ERole role) {
String name,
String email,
String password,
String number,
ERole role,
String carId,
String vin,
String pincode,
String latitude,
String longitude) {
UserData userData = new UserData();
VehicleDetails vehicleDetails = null;
UserDetails userDetails = null;
try {
User user = new User(email, number, encoder.encode(password), role);
user = userRepository.save(user);
user = userRepository.findById(user.getId()).get();
userDetails = userData.getPredefineUser(name, user);
userDetailsRepository.save(userDetails);
vehicleDetails = vehicleService.createVehicle();
userDetails = userDetailsRepository.save(userDetails);
vehicleDetails = createVehicle(carId, vin, pincode, latitude, longitude);
if (vehicleDetails != null) {
vehicleDetails.setOwner(user);
vehicleDetailsRepository.save(vehicleDetails);
return true;
} else {
logger.error("Fail to create vehicle for user {}", email);
return false;
}
logger.error("Fail to create vehicle for user {}", email);
return false;
// generate random bytes
byte[] videoBytes = new byte[10];
random.nextBytes(videoBytes);
String videoName = userDetails.getName().replace(" ", "_") + "_video";
String conversionParam = "-v codec h264";
ProfileVideo profileVideo = new ProfileVideo(videoName, videoBytes, user);
profileVideo.setConversion_params(conversionParam);
profileVideoRepository.save(profileVideo);
return true;
} catch (Exception e) {
logger.error("Fail to create user {}, Exception :: {}", email, e);
return false;
Expand Down
58 changes: 53 additions & 5 deletions services/identity/src/main/java/com/crapi/constant/TestUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,63 @@ public class TestUsers {
public TestUsers() {
users.add(
new SeedUser(
"Adam", "adam007@example.com", "9876895423", "adam007!123", ERole.ROLE_PREDEFINE));
"Adam",
"adam007@example.com",
"9876895423",
"adam007!123",
ERole.ROLE_PREDEFINE,
"f89b5f21-7829-45cb-a650-299a61090378",
"7ECOX34KJTV359804",
"123456",
"32.778889",
"-91.919243"));
users.add(
new SeedUser(
"Pogba", "pogba006@example.com", "9876570006", "pogba006!123", ERole.ROLE_PREDEFINE));
"Pogba",
"pogba006@example.com",
"9876570006",
"pogba006!123",
ERole.ROLE_PREDEFINE,
"cd515c12-0fc1-48ae-8b61-9230b70a845b",
"8VAUI03PRUQ686911",
"123456",
"31.284788",
"-92.471176"));
users.add(
new SeedUser(
"Robot", "robot001@example.com", "9876570001", "robot001!123", ERole.ROLE_PREDEFINE));
users.add(new SeedUser("Test", "test@example.com", "9876540001", "Test!123", ERole.ROLE_USER));
"Robot",
"robot001@example.com",
"9876570001",
"robot001!123",
ERole.ROLE_PREDEFINE,
"4bae9968-ec7f-4de3-a3a0-ba1b2ab5e5e5",
"0NKPZ09IHOP508673",
"123456",
"37.746880",
"-84.301460"));
users.add(
new SeedUser("Admin", "admin@example.com", "9010203040", "Admin!123", ERole.ROLE_ADMIN));
new SeedUser(
"Test",
"test@example.com",
"9876540001",
"Test!123",
ERole.ROLE_USER,
"1929186d-8b67-4163-a208-de52a41f7301",
"8IGEF39BZUJ159285",
"123456",
"38.206348",
"-84.270172"));
users.add(
new SeedUser(
"Admin",
"admin@example.com",
"9010203040",
"Admin!123",
ERole.ROLE_ADMIN,
"f5c506f5-3af2-4120-926c-64ad8b10ddc2",
"6NBBY70FWUM324316",
"123456",
"37.406769",
"-94.705528"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public class VehicleDetails {
private long id;

@Column(name = "uuid", updatable = false, nullable = false, unique = true)
private UUID uuid = UUID.randomUUID();
private UUID uuid;

private String pincode;
private String vin;
private long year;
private EStatus status;
@Transient List<VehicleOwnership> previousOwners;

@OneToOne(cascade = CascadeType.ALL)
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "vehicle_model_id")
private VehicleModel model;

Expand All @@ -54,7 +54,16 @@ public class VehicleDetails {
private User owner;

public VehicleDetails(String pincode, String vin) {
this.uuid = UUID.randomUUID();
this.pincode = pincode;
this.vin = vin;
this.status = EStatus.ACTIVE;
this.year = LocalDate.now().getYear();
this.previousOwners = Arrays.asList();
}

public VehicleDetails(String uuid, String pincode, String vin) {
this.uuid = UUID.fromString(uuid);
this.pincode = pincode;
this.vin = vin;
this.status = EStatus.ACTIVE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class VehicleModel implements Serializable {
private EFuelType fuel_type;
private String vehicle_img;

@OneToOne(cascade = CascadeType.ALL)
@ManyToOne(cascade = CascadeType.ALL)
private VehicleCompany vehiclecompany;

public VehicleModel() {}
Expand Down
Loading

0 comments on commit 2fdda54

Please sign in to comment.