Skip to content

Commit

Permalink
format code using spotles
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuchitO committed Mar 20, 2024
1 parent f576e31 commit 1200644
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 71 deletions.
3 changes: 3 additions & 0 deletions kbazaar/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ clean:
run:
./gradlew bootRun

format:
./gradlew spotlessApply

health:
curl -X GET http://localhost:8080/api/v1/health

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
@SpringBootApplication
public class KBazaarApplication {

public static void main(String[] args) {
SpringApplication.run(KBazaarApplication.class, args);
}

public static void main(String[] args) {
SpringApplication.run(KBazaarApplication.class, args);
}
}
1 change: 0 additions & 1 deletion kbazaar/src/main/java/com/kampus/kbazaar/cart/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.kampus.kbazaar.product.Product;
import lombok.Data;


@Data
public class Cart {
private int userID;
Expand Down
47 changes: 26 additions & 21 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,38 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v1")
public class CartController {
public static Cart[] carts = new Cart[]{
new Cart(1, new Product[]{
new Product("sku1", "product1", 100, 1),
}),
new Cart(2, new Product[]{
new Product("sku2", "product2", 200, 2),
}),
new Cart(3, new Product[]{
new Product("sku3", "product3", 300, 3),
}),
};

public static Cart[] carts =
new Cart[] {
new Cart(
1,
new Product[] {
new Product("sku1", "product1", 100, 1),
}),
new Cart(
2,
new Product[] {
new Product("sku2", "product2", 200, 2),
}),
new Cart(
3,
new Product[] {
new Product("sku3", "product3", 300, 3),
}),
};

@PostMapping("/carts/{id}/additems")
public ResponseEntity addProductToCart(@PathVariable String id, @RequestBody Product[] products) {
Cart cart = new Cart(1, new Product[]{
new Product("sku1", "product1", 100, 1),
});
public ResponseEntity addProductToCart(
@PathVariable String id, @RequestBody Product[] products) {
Cart cart =
new Cart(
1,
new Product[] {
new Product("sku1", "product1", 100, 1),
});

return ResponseEntity.ok(cart);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public class HealthController {
public String health() {
return "{ \"status\": \"alive\" }";
}
}
}
21 changes: 10 additions & 11 deletions kbazaar/src/main/java/com/kampus/kbazaar/product/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
@Data
public class Product {
private String name;
private String sku;
private String sku;

private double price;
private int quantity;
private double price;
private int quantity;

public Product() {
}
public Product() {}

public Product(String sku, String name, double price, int quantity) {
this.name = name;
this.price = price;
this.sku = sku;
this.quantity = quantity;
}
public Product(String sku, String name, double price, int quantity) {
this.name = name;
this.price = price;
this.sku = sku;
this.quantity = quantity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1")
public class ProductController {
public static Product[] productsDB = new Product[] {
new Product("sku1", "Product 1", 100.0, 10),
new Product("sku2", "Product 2", 200.0, 20),
new Product("sku3", "Product 3", 300.0, 30)
};
public static Product[] productsDB =
new Product[] {
new Product("sku1", "Product 1", 100.0, 10),
new Product("sku2", "Product 2", 200.0, 20),
new Product("sku3", "Product 3", 300.0, 30)
};

@GetMapping("/products")
public ResponseEntity getProducts() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.kampus.kbazaar.user;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -9,10 +8,11 @@
@RestController
@RequestMapping("/api/v1")
public class UserController {
private User[] users = new User[]{
new User(1, "John Doe", "[email protected]"),
new User(2, "Jane Doe", "[email protected]"),
};
private User[] users =
new User[] {
new User(1, "John Doe", "[email protected]"),
new User(2, "Jane Doe", "[email protected]"),
};

@GetMapping("/users")
public User[] getAllUsers() {
Expand All @@ -27,7 +27,7 @@ public User getUserById(@PathVariable String id) {
return user;
}
}
// how to return ? {"message": "User not found"}
// how to return ? {"message": "User not found"}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
@SpringBootTest
class KBazaarApplicationTests {

@Test
void contextLoads() {
}

@Test
void contextLoads() {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kampus.kbazaar.architecture;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
Expand All @@ -8,24 +10,30 @@
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

@AnalyzeClasses(packages = "com.kampus.kbazaar", importOptions = ImportOption.DoNotIncludeTests.class)
@AnalyzeClasses(
packages = "com.kampus.kbazaar",
importOptions = ImportOption.DoNotIncludeTests.class)
public class DependencyRuleTest {

@ArchTest
static final ArchRule service_should_access_by_controller =
classes().that()
classes()
.that()
.areAnnotatedWith(Service.class)
.should().onlyBeAccessed().byClassesThat()
.should()
.onlyBeAccessed()
.byClassesThat()
.areAnnotatedWith(RestController.class)
.allowEmptyShould(true);

@ArchTest
static final ArchRule repository_should_access_by_service =
classes().that()
classes()
.that()
.areAnnotatedWith(Repository.class)
.should().onlyBeAccessed().byClassesThat()
.should()
.onlyBeAccessed()
.byClassesThat()
.areAnnotatedWith(Service.class)
.allowEmptyShould(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kampus.kbazaar.architecture;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
Expand All @@ -8,27 +10,33 @@
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

@AnalyzeClasses(packages = "com.kampus.kbazaar", importOptions = ImportOption.DoNotIncludeTests.class)
@AnalyzeClasses(
packages = "com.kampus.kbazaar",
importOptions = ImportOption.DoNotIncludeTests.class)
public class NamingRuleTest {
@ArchTest
static ArchRule rest_controller_naming_should_ending_with_controller =
classes()
.that().areAnnotatedWith(RestController.class)
.should().haveSimpleNameEndingWith("Controller");
.that()
.areAnnotatedWith(RestController.class)
.should()
.haveSimpleNameEndingWith("Controller");

@ArchTest
static ArchRule repository_naming_should_ending_repository =
classes()
.that().areAnnotatedWith(Repository.class)
.should().haveSimpleNameEndingWith("Repository")
.that()
.areAnnotatedWith(Repository.class)
.should()
.haveSimpleNameEndingWith("Repository")
.allowEmptyShould(true);

@ArchTest
static ArchRule service_naming_should_ending_service =
classes()
.that().areAnnotatedWith(Service.class)
.should().haveSimpleNameEndingWith("Service")
.that()
.areAnnotatedWith(Service.class)
.should()
.haveSimpleNameEndingWith("Service")
.allowEmptyShould(true);
}

0 comments on commit 1200644

Please sign in to comment.