Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback and Feedback Category CRUD completed. #4

Merged
merged 8 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
12 changes: 9 additions & 3 deletions src/main/java/com/waa/project/Application.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.waa.project;

import org.modelmapper.ModelMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

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

@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.waa.project.controller;

import com.waa.project.dto.FeedbackCategoryDto;
import com.waa.project.service.FeedbackCategoryService;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping("/api/v1/feedbackcategories")
public class FeedbackCategoryController {

@Autowired
private FeedbackCategoryService feedbackCategoryService;

@GetMapping
public List<FeedbackCategoryDto> getAllFeedCategory() {
return feedbackCategoryService.getAllCategories();
}

@GetMapping("/{catId}")
public FeedbackCategoryDto getAllFeedCategory(@PathVariable Long catId) {
return feedbackCategoryService.getCategory(catId);
}

@PutMapping("/{catId}")
public String getAllFeedCategory(
@RequestBody FeedbackCategoryDto feedbackCategoryDto,
@PathVariable Long catId
) {
return feedbackCategoryService.update(feedbackCategoryDto, catId);
}

@PostMapping
public String getAllFeedCategory(@RequestBody FeedbackCategoryDto feedbackCategoryDto) {
return feedbackCategoryService.save(feedbackCategoryDto);
}

@DeleteMapping("/{catId}")
public String delete(@PathVariable Long catId) {
return feedbackCategoryService.delete(catId);
}


}
63 changes: 63 additions & 0 deletions src/main/java/com/waa/project/controller/FeedbackController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.waa.project.controller;

import com.waa.project.dto.FeedbackDto;
import com.waa.project.service.FeedbackService;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping("/api/v1/feedbacks")
public class FeedbackController {

@Autowired
private FeedbackService feedbackService;

@GetMapping
public List<FeedbackDto> getAllFeedBack() {

return feedbackService.getAllFeedbacks();
}

@GetMapping("/category/{Id}")
public List<FeedbackDto> getFeedByCategory(@PathVariable Long Id) {
return feedbackService.findFeedbackByCategory(Id);
}

@PutMapping("/{Id}")
public String update(
@RequestBody FeedbackDto feedbackDto, @PathVariable Long Id
) {
return feedbackService.update(feedbackDto, Id);
}

@PostMapping
public String save(@RequestBody FeedbackDto feedbackDto) {
return feedbackService.save(feedbackDto);
}

@DeleteMapping("/{Id}")
public String delete(@PathVariable Long Id) {

return feedbackService.delete(Id);
}

@GetMapping("/reports")
public Map<String, Integer> findFeedbackByCategoryCount() {
List<Object[]> results = feedbackService.findFeedbackByCategoryCount();
return results.stream()
.collect(Collectors.toMap(
result -> (String) result[0],
result -> ((Long) result[1]).intValue()
));

}

}
10 changes: 10 additions & 0 deletions src/main/java/com/waa/project/dto/FeedbackCategoryDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.waa.project.dto;

import lombok.Data;

@Data
public class FeedbackCategoryDto {
private Long id;
private String name;
private String description;
}
14 changes: 14 additions & 0 deletions src/main/java/com/waa/project/dto/FeedbackDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.waa.project.dto;

import com.waa.project.entity.FeedbackCategory;
import com.waa.project.entity.Student;
import lombok.Data;

@Data
public class FeedbackDto {
private Long id;
private String title;
private String body;
private FeedbackCategory feedbackCategory;
private Student student;
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.waa.project.repository;

import com.waa.project.entity.FeedbackCategory;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface FeedbackCategoryRepository extends ListCrudRepository<FeedbackCategory, Long> {
}
19 changes: 19 additions & 0 deletions src/main/java/com/waa/project/repository/FeedbackRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.waa.project.repository;

import com.waa.project.entity.Feedback;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface FeedbackRepository extends ListCrudRepository<Feedback, Long> {
List<Feedback> findFeedbackByCategoryId(Long id);

@Query("SELECT fc.name, COUNT(f.category) " +
"FROM Feedback f " +
"JOIN f.category fc " +
"GROUP BY fc.name")
List<Object[]> findFeedbackByCategoryCount();
}
10 changes: 10 additions & 0 deletions src/main/java/com/waa/project/repository/StudentRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.waa.project.repository;


import com.waa.project.entity.Student;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentRepository extends ListCrudRepository<Student, Long> {
}
Empty file.
17 changes: 17 additions & 0 deletions src/main/java/com/waa/project/service/FeedbackCategoryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.waa.project.service;

import com.waa.project.dto.FeedbackCategoryDto;

import java.util.List;

public interface FeedbackCategoryService {
List<FeedbackCategoryDto> getAllCategories();

FeedbackCategoryDto getCategory(Long feedId);

String save(FeedbackCategoryDto category);

String update(FeedbackCategoryDto category, Long fid);

String delete(Long fid);
}
21 changes: 21 additions & 0 deletions src/main/java/com/waa/project/service/FeedbackService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.waa.project.service;

import com.waa.project.dto.FeedbackDto;

import java.util.List;

public interface FeedbackService {
List<FeedbackDto> getAllFeedbacks();

FeedbackDto getFeedback(Long feedId);

String save(FeedbackDto feedback);

String update(FeedbackDto feedback, Long fid);

String delete(Long fid);

List<FeedbackDto> findFeedbackByCategory(Long id);

List<Object[]> findFeedbackByCategoryCount();
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.waa.project.service.impl;

import com.waa.project.dto.FeedbackCategoryDto;
import com.waa.project.entity.FeedbackCategory;
import com.waa.project.repository.FeedbackCategoryRepository;
import com.waa.project.service.FeedbackCategoryService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

@Service
public class FeebackCategoryServiceImp implements FeedbackCategoryService {

@Autowired
private FeedbackCategoryRepository feedbackCategoryRepository;

@Autowired
private ModelMapper modelMapper;

@Override
public List<FeedbackCategoryDto> getAllCategories() {
List<FeedbackCategoryDto> feedbackCategories = new ArrayList<>();
feedbackCategoryRepository.findAll().forEach(feed -> feedbackCategories.add(modelMapper.map(
feed, FeedbackCategoryDto.class)));

return feedbackCategories;
}

@Override
public FeedbackCategoryDto getCategory(Long feedId) {

return modelMapper.map(feedbackCategoryRepository.findById(feedId), FeedbackCategoryDto.class);
}

@Override
public String save(FeedbackCategoryDto feedback) {
feedbackCategoryRepository.save(modelMapper.map(feedback, FeedbackCategory.class));
return "FeedbackCategory saved successfully.";
}

@Override
public String update(FeedbackCategoryDto feedback, Long fid) {
FeedbackCategory feedToUpdate = feedbackCategoryRepository.findById(fid)
.orElseThrow(
() -> new NoSuchElementException(
"No feedback was found."));
feedToUpdate.setName(feedback.getName());
feedToUpdate.setDescription(feedback.getDescription());
feedbackCategoryRepository.save(feedToUpdate);
return "FeedbackCategory is updated.";
}

@Override
public String delete(Long fid) {
feedbackCategoryRepository.deleteById(fid);
return "FeedbackCategory is deleted.";
}
}
Loading