Skip to content

Commit

Permalink
added category dto, controller, service
Browse files Browse the repository at this point in the history
  • Loading branch information
ved-asole committed Feb 25, 2024
1 parent 09037f3 commit 09562a5
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 10 deletions.
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);
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package com.vedasole.ekartecommercebackend.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import javax.persistence.*;
import lombok.*;
import lombok.Builder;
import lombok.Data;
import org.springframework.validation.annotation.Validated;

@AllArgsConstructor
@NoArgsConstructor
@Data
import javax.persistence.*;
import java.io.Serial;
import java.io.Serializable;

/**
* Entity class for Category
*/
@Builder
@Validated
@Entity
@Data
@Table(name = "category")
public class Category {
public class Category implements Serializable {

@Serial
private static final long serialVersionUID = -5392075886775352349L;

@Id
@Column(updatable = false)
Expand All @@ -24,13 +32,89 @@ public class Category {
private String name;

private String image;

@Column(length = 1000)
private String desc;

@ManyToOne
@JoinColumn(name = "parent_category_id")
private Category parentCategory;

@JsonProperty
private boolean isActive;
private boolean active;

public Category() {}

public Category(String name, String image, String desc, Category parentCategory, boolean active) {
this.name = name;
this.image = image;
this.desc = desc;
this.parentCategory = parentCategory;
this.active = active;
}

public Category(long categoryId, String name, String image, String desc, Category parentCategory, boolean active) {
this.categoryId = categoryId;
this.name = name;
this.image = image;
this.desc = desc;
this.parentCategory = parentCategory;
this.active = active;
}
//
// public long getCategoryId() {
// return categoryId;
// }
//
// public void setCategoryId(long categoryId) {
// this.categoryId = categoryId;
// }
//
// public String getName() {
// return name;
// }
//
// public void setName(String name) {
// this.name = name;
// }
//
// public String getImage() {
// return image;
// }
//
// public void setImage(String image) {
// this.image = image;
// }
//
// public String getDesc() {
// return desc;
// }
//
// public void setDesc(String desc) {
// this.desc = desc;
// }
//
// public Category getParentCategory() {
// return parentCategory;
// }
//
// public void setParentCategory(Category parentCategory) {
// this.parentCategory = parentCategory;
// }
//
// @Override
// public final boolean equals(Object o) {
// if (this == o) return true;
// if (o == null) return false;
// Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
// Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
// if (thisEffectiveClass != oEffectiveClass) return false;
// Category category = (Category) o;
// return Long.valueOf(this.getCategoryId()) != null && Objects.equals(getCategoryId(), category.getCategoryId());
// }
//
// @Override
// public final int hashCode() {
// return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
// }
}
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;

}
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> {
}
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);
}
}
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);

}

0 comments on commit 09562a5

Please sign in to comment.