-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added category dto, controller, service
- Loading branch information
Showing
6 changed files
with
316 additions
and
10 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
src/main/java/com/vedasole/ekartecommercebackend/controller/CategoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.vedasole.ekartecommercebackend.controller; | ||
|
||
import com.vedasole.ekartecommercebackend.payload.ApiResponse; | ||
import com.vedasole.ekartecommercebackend.payload.CategoryDto; | ||
import com.vedasole.ekartecommercebackend.service.serviceInterface.CategoryService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
import javax.validation.Valid; | ||
import java.util.List; | ||
|
||
@Validated | ||
@RestController | ||
@RequestMapping("/api/v1/categories") | ||
@CrossOrigin("http://localhost:5173") | ||
public class CategoryController { | ||
|
||
private final CategoryService categoryService; | ||
|
||
|
||
public CategoryController(CategoryService categoryService) { | ||
this.categoryService = categoryService; | ||
} | ||
|
||
/** | ||
* Creates a new category. | ||
* | ||
* @param categoryDto the category details | ||
* @return the created category | ||
*/ | ||
@PostMapping | ||
public ResponseEntity<CategoryDto> createCategory( | ||
@Valid @RequestBody CategoryDto categoryDto) { | ||
CategoryDto createdCategory = this.categoryService.createCategory(categoryDto); | ||
return new ResponseEntity<>(createdCategory, HttpStatus.CREATED); | ||
} | ||
|
||
/** | ||
* Updates an existing category. | ||
* | ||
* @param categoryDto the updated category details | ||
* @param categoryId the ID of the category to update | ||
* @return the updated category | ||
*/ | ||
@PutMapping("/{categoryId}") | ||
public ResponseEntity<CategoryDto> updateCategory( | ||
@Valid @RequestBody CategoryDto categoryDto, | ||
@PathVariable Long categoryId) { | ||
CategoryDto updatedCategory = this.categoryService.updateCategory(categoryDto, categoryId); | ||
return new ResponseEntity<>(updatedCategory, HttpStatus.OK); | ||
} | ||
|
||
/** | ||
* Deletes an existing category. | ||
* | ||
* @param categoryId the ID of the category to delete | ||
* @return an ApiResponse indicating whether the deletion was successful or not | ||
*/ | ||
@DeleteMapping("/{categoryId}") | ||
public ResponseEntity<ApiResponse> deleteCategory( | ||
@PathVariable Long categoryId) { | ||
this.categoryService.deleteCategory(categoryId); | ||
return new ResponseEntity<>( | ||
new ApiResponse( | ||
"Category deleted successfully", | ||
true), | ||
HttpStatus.OK); | ||
} | ||
|
||
/** | ||
* Returns a specific category based on its ID. | ||
* | ||
* @param categoryId the ID of the category to retrieve | ||
* @return the requested category, or a 404 Not Found error if the category does not exist | ||
*/ | ||
@GetMapping("/{categoryId}") | ||
public ResponseEntity<CategoryDto> getCategory( | ||
@PathVariable Long categoryId | ||
) { | ||
CategoryDto category = this.categoryService.getCategoryById(categoryId); | ||
return category == null ? | ||
new ResponseEntity<>(HttpStatus.NOT_FOUND) : | ||
new ResponseEntity<>(category, HttpStatus.OK); | ||
} | ||
|
||
/** | ||
* Returns a list of all categories. | ||
* | ||
* @return a list of all categories | ||
*/ | ||
@GetMapping | ||
public ResponseEntity<List<CategoryDto>> getAllCategories(){ | ||
List<CategoryDto> allCategories = this.categoryService. | ||
getAllCategories(); | ||
return new ResponseEntity<>(allCategories, HttpStatus.OK); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 17 additions & 3 deletions
20
src/main/java/com/vedasole/ekartecommercebackend/payload/CategoryDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
package com.vedasole.ekartecommercebackend.payload; | ||
|
||
import com.vedasole.ekartecommercebackend.entity.Category; | ||
import lombok.*; | ||
|
||
import java.io.Serial; | ||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
/** | ||
* DTO for {@link com.vedasole.ekartecommercebackend.entity.Category} | ||
* DTO for {@link Category} | ||
*/ | ||
public record CategoryDto(long categoryId, String name, String image, String desc, | ||
long parentCategory, boolean isActive) implements Serializable { | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Data | ||
public class CategoryDto implements Serializable { | ||
|
||
@Serial | ||
private static final long serialVersionUID = -6361844320830928689L; | ||
private long categoryId; | ||
private String name; | ||
private String image; | ||
private String desc; | ||
private Category parentCategory; | ||
private boolean active; | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/vedasole/ekartecommercebackend/repository/CategoryRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.vedasole.ekartecommercebackend.repository; | ||
|
||
import com.vedasole.ekartecommercebackend.entity.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
|
||
@Repository | ||
public interface CategoryRepo extends JpaRepository<Category, Long> { | ||
} |
80 changes: 80 additions & 0 deletions
80
...main/java/com/vedasole/ekartecommercebackend/service/serviceImpl/CategoryServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.vedasole.ekartecommercebackend.service.serviceImpl; | ||
|
||
import com.vedasole.ekartecommercebackend.entity.Category; | ||
import com.vedasole.ekartecommercebackend.exception.ResourceNotFoundException; | ||
import com.vedasole.ekartecommercebackend.payload.CategoryDto; | ||
import com.vedasole.ekartecommercebackend.repository.CategoryRepo; | ||
import com.vedasole.ekartecommercebackend.service.serviceInterface.CategoryService; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.stereotype.Service; | ||
import java.util.List; | ||
|
||
@Service | ||
public class CategoryServiceImpl implements CategoryService { | ||
|
||
private final CategoryRepo categoryRepo; | ||
private final ModelMapper modelMapper; | ||
|
||
public CategoryServiceImpl(CategoryRepo categoryRepo, ModelMapper modelMapper) { | ||
this.categoryRepo = categoryRepo; | ||
this.modelMapper = modelMapper; | ||
} | ||
|
||
@Override | ||
public CategoryDto createCategory(CategoryDto categoryDto) { | ||
Category addedCategory = this.categoryRepo.save(dtoToCategory(categoryDto)); | ||
return categoryToDto(addedCategory); | ||
} | ||
|
||
@Override | ||
public CategoryDto updateCategory(CategoryDto categoryDto, Long categoryId) { | ||
Category category = dtoToCategory(categoryDto); | ||
Category categoryInDB = this.categoryRepo.findById(categoryId). | ||
orElseThrow(() -> new ResourceNotFoundException( | ||
"Category", "id" , categoryId)); | ||
categoryInDB.setCategoryId(category.getCategoryId()); | ||
categoryInDB.setName(category.getName()); | ||
categoryInDB.setImage(category.getImage()); | ||
categoryInDB.setDesc(category.getDesc()); | ||
categoryInDB.setParentCategory(category.getParentCategory()); | ||
categoryInDB.setActive(category.isActive()); | ||
|
||
this.categoryRepo.save(categoryInDB); | ||
|
||
return categoryToDto(categoryInDB); | ||
} | ||
|
||
@Override | ||
public List<CategoryDto> getAllCategories() { | ||
return this.categoryRepo.findAll().stream() | ||
.map(this::categoryToDto) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public CategoryDto getCategoryById(Long categoryId) { | ||
Category category = this.categoryRepo.findById(categoryId). | ||
orElseThrow(() -> new ResourceNotFoundException( | ||
"Category", "id" , categoryId)); | ||
|
||
return categoryToDto(category); | ||
} | ||
|
||
@Override | ||
public boolean deleteCategory(Long categoryId) { | ||
try{ | ||
this.categoryRepo.deleteById(categoryId); | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
private Category dtoToCategory(CategoryDto categoryDto){ | ||
return this.modelMapper.map(categoryDto, Category.class); | ||
} | ||
private CategoryDto categoryToDto(Category category) | ||
{ | ||
return this.modelMapper.map(category, CategoryDto.class); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ain/java/com/vedasole/ekartecommercebackend/service/serviceInterface/CategoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.vedasole.ekartecommercebackend.service.serviceInterface; | ||
|
||
import com.vedasole.ekartecommercebackend.payload.CategoryDto; | ||
|
||
import java.util.List; | ||
|
||
public interface CategoryService { | ||
|
||
public CategoryDto createCategory(CategoryDto categoryDto); | ||
|
||
public CategoryDto updateCategory(CategoryDto categoryDto , Long categoryId); | ||
|
||
public List<CategoryDto> getAllCategories(); | ||
|
||
public CategoryDto getCategoryById(Long categoryId); | ||
|
||
boolean deleteCategory(Long categoryId); | ||
|
||
} |