Skip to content

Commit e0fcea1

Browse files
committed
Category CRUD is done successfully.
1 parent e33ebdc commit e0fcea1

File tree

10 files changed

+262
-0
lines changed

10 files changed

+262
-0
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
<artifactId>lombok</artifactId>
4343
<optional>true</optional>
4444
</dependency>
45+
<!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
46+
<dependency>
47+
<groupId>org.modelmapper</groupId>
48+
<artifactId>modelmapper</artifactId>
49+
<version>3.1.0</version>
50+
</dependency>
51+
4552
<dependency>
4653
<groupId>org.springframework.boot</groupId>
4754
<artifactId>spring-boot-starter-test</artifactId>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.manir.springbootecommercerestapi;
22

3+
import org.modelmapper.ModelMapper;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
57

68
@SpringBootApplication
79
public class SpringBootECommerceRestApiApplication {
810

911
public static void main(String[] args) {
12+
1013
SpringApplication.run(SpringBootECommerceRestApiApplication.class, args);
1114
}
1215

16+
@Bean
17+
public ModelMapper modelMapper(){
18+
return new ModelMapper();
19+
}
20+
1321
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.manir.springbootecommercerestapi.controller;
2+
3+
import com.manir.springbootecommercerestapi.dto.CategoryDto;
4+
import com.manir.springbootecommercerestapi.service.CategoryService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
import java.util.List;
11+
12+
13+
@RestController
14+
@RequestMapping(value = "api/v1/categories")
15+
public class CategoryController {
16+
17+
@Autowired
18+
private CategoryService categoryService;
19+
20+
//create category api
21+
@PostMapping("/createCategory")
22+
public ResponseEntity<CategoryDto> createCategory(@RequestBody CategoryDto categoryDto){
23+
CategoryDto responseCategory = categoryService.createCategory(categoryDto);
24+
return new ResponseEntity<>(responseCategory, HttpStatus.CREATED);
25+
}
26+
27+
//get all categories api
28+
@GetMapping("/getAllCategory")
29+
public List<CategoryDto> getAllCategory(){
30+
List<CategoryDto> categoryDtoList = categoryService.getAllCategory();
31+
return categoryDtoList;
32+
}
33+
34+
//get category by id api
35+
@GetMapping("{categoryId}")
36+
public ResponseEntity<CategoryDto> getCatecoryById(@PathVariable Long categoryId){
37+
CategoryDto responseCategory = categoryService.getCategoryById(categoryId);
38+
return ResponseEntity.ok(responseCategory);
39+
}
40+
41+
//update category api
42+
@PutMapping("/updateCategory/{categoryId}")
43+
public ResponseEntity<CategoryDto> updateCategory(@RequestBody CategoryDto categoryDto,
44+
@PathVariable Long categoryId){
45+
CategoryDto responseCategory = categoryService.updateCategory(categoryDto, categoryId);
46+
return new ResponseEntity<>(responseCategory, HttpStatus.OK);
47+
}
48+
49+
//delete category api
50+
@DeleteMapping("/deleteCategory/{categoryId}")
51+
public ResponseEntity<String> deleteCategory(@PathVariable Long categoryId){
52+
categoryService.deleteCategory(categoryId);
53+
return ResponseEntity.ok("Category with id: "+ categoryId + " is deleted successfully:)");
54+
}
55+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.manir.springbootecommercerestapi.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class CategoryDto {
7+
8+
private Long id;
9+
private String title;
10+
private String keywords;
11+
private String description;
12+
private String status;
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.manir.springbootecommercerestapi.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
7+
public class ResourceNotFoundException extends RuntimeException{
8+
private String resourceName;
9+
private Long fieldValue;
10+
11+
public ResourceNotFoundException(String resourceName, Long fieldValue) {
12+
super(String.format("%s with id : %s is not found !", resourceName, fieldValue));
13+
this.resourceName = resourceName;
14+
this.fieldValue = fieldValue;
15+
}
16+
17+
public String getResourceName() {
18+
return resourceName;
19+
}
20+
21+
public Long getFieldValue() {
22+
return fieldValue;
23+
}
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.manir.springbootecommercerestapi.repository;
2+
3+
import com.manir.springbootecommercerestapi.resource.Category;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface CategoryRepository extends JpaRepository<Category, Long> {
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.manir.springbootecommercerestapi.resource;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import javax.persistence.*;
8+
9+
@AllArgsConstructor
10+
@NoArgsConstructor
11+
@Data
12+
@Entity
13+
@Table(name = "categories")
14+
public class Category {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
private Long id;
19+
@Column(name = "title")
20+
private String title;
21+
@Column(name = "keywords")
22+
private String keywords;
23+
@Column(name = "description")
24+
private String description;
25+
@Column(name = "status")
26+
private String status;
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.manir.springbootecommercerestapi.service;
2+
3+
import com.manir.springbootecommercerestapi.dto.CategoryDto;
4+
5+
import java.util.List;
6+
7+
public interface CategoryService {
8+
9+
CategoryDto createCategory(CategoryDto categoryDto);
10+
List<CategoryDto> getAllCategory();
11+
CategoryDto getCategoryById(Long categoryId);
12+
CategoryDto updateCategory(CategoryDto categoryDto, Long categoryId);
13+
void deleteCategory(Long categoryId);
14+
15+
16+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.manir.springbootecommercerestapi.service.Impl;
2+
3+
import com.manir.springbootecommercerestapi.dto.CategoryDto;
4+
import com.manir.springbootecommercerestapi.exception.ResourceNotFoundException;
5+
import com.manir.springbootecommercerestapi.repository.CategoryRepository;
6+
import com.manir.springbootecommercerestapi.resource.Category;
7+
import com.manir.springbootecommercerestapi.service.CategoryService;
8+
import org.modelmapper.ModelMapper;
9+
import org.springframework.stereotype.Service;
10+
11+
import javax.annotation.Resource;
12+
import java.util.List;
13+
import java.util.stream.Collectors;
14+
15+
@Service
16+
public class CategoryServiceImpl implements CategoryService {
17+
18+
@Resource(name = "categoryRepository")
19+
private CategoryRepository categoryRepository;
20+
@Resource(name = "modelMapper")
21+
private ModelMapper modelMapper;
22+
23+
@Override
24+
public CategoryDto createCategory(CategoryDto categoryDto) {
25+
//map dto to entity
26+
Category category = mapToEntity(categoryDto);
27+
//save entity
28+
Category newCategory = categoryRepository.save(category);
29+
//map back entity to dto
30+
CategoryDto responseCategoryDto = mapToDto(newCategory);
31+
32+
return responseCategoryDto;
33+
}
34+
35+
@Override
36+
public List<CategoryDto> getAllCategory() {
37+
38+
List<Category> categories = categoryRepository.findAll();
39+
//map all categories to dto
40+
List<CategoryDto> categoryDtoList = categories.stream()
41+
.map(category -> mapToDto(category))
42+
.collect(Collectors.toList());
43+
44+
return categoryDtoList;
45+
}
46+
47+
@Override
48+
public CategoryDto getCategoryById(Long categoryId) {
49+
50+
Category category = categoryRepository.findById(categoryId)
51+
.orElseThrow(
52+
() -> new ResourceNotFoundException("Category", categoryId)
53+
);
54+
//map entity to dto
55+
CategoryDto responseCategory = mapToDto(category);
56+
57+
return responseCategory;
58+
}
59+
60+
@Override
61+
public CategoryDto updateCategory(CategoryDto categoryDto, Long categoryId) {
62+
63+
Category category = categoryRepository.findById(categoryId)
64+
.orElseThrow(
65+
() -> new ResourceNotFoundException("Category", categoryId)
66+
);
67+
68+
category.setTitle(categoryDto.getTitle());
69+
category.setDescription(categoryDto.getDescription());
70+
category.setKeywords(categoryDto.getKeywords());
71+
category.setStatus(categoryDto.getStatus());
72+
Category updatedCategory = categoryRepository.save(category);
73+
//map entity to dto
74+
CategoryDto responseCategory = mapToDto(updatedCategory);
75+
return responseCategory;
76+
}
77+
78+
@Override
79+
public void deleteCategory(Long categoryId) {
80+
Category category = categoryRepository.findById(categoryId)
81+
.orElseThrow(
82+
() -> new ResourceNotFoundException("Category", categoryId)
83+
);
84+
categoryRepository.delete(category);
85+
}
86+
87+
private CategoryDto mapToDto(Category category){
88+
CategoryDto categoryDto = modelMapper.map(category, CategoryDto.class);
89+
return categoryDto;
90+
}
91+
92+
private Category mapToEntity(CategoryDto categoryDto){
93+
Category category = modelMapper.map(categoryDto, Category.class);
94+
return category;
95+
}
96+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1+
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
2+
spring.jpa.show-sql =true
3+
spring.jpa.properties.hibernate.format_sql= true
4+
spring.datasource.password=Password123#@!
5+
spring.datasource.username=root
6+
spring.datasource.url=jdbc:mysql://localhost:3306/springBootEcommerceRestApi?serverTimezone=UTC
7+
spring.jpa.hibernate.ddl-auto=update
8+
9+
#logging.level.org.springframework.security=DEBUG
110

0 commit comments

Comments
 (0)