Commit 7a3cbfcc by Ninh Hoang Cuong

get feedback and get feedback by id

parent e3bc6200
package com.feedback.demo.controllers;
import com.feedback.demo.dto.FeedBackDTO;
import com.feedback.demo.service.FeedBackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/feedback")
@Tag(name = "Feed Back")
public class FeedBackController {
@Autowired
private FeedBackService feedBackService;
@RequestMapping(value = {"/show/", "/show"})
public void redirect(HttpServletResponse response) throws IOException {
response.sendRedirect("/feedback/show/1");
}
@Operation(description = "Get feed back", responses = {
@ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = FeedBackDTO.class))), responseCode = "200")
})
@GetMapping("/list")
public ResponseEntity<List<FeedBackDTO>> getFeedBack(
@RequestParam(value = "page", required = false, defaultValue = "0") String pageIndex
) {
return ResponseEntity.ok(feedBackService.getFeedback(pageIndex));
}
@Operation(description = "Get feed back by id", responses = {
@ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation =FeedBackDTO.class))), responseCode = "200")
})
@GetMapping("/show/{id}")
public FeedBackDTO getFeedbackById(
@PathVariable(name = "id", required = false) String id
) {
return feedBackService.getFeedBackById(id);
}
}
package com.feedback.demo.controllers;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Tag(name = "Feed Back Options")
@RequestMapping("/feedback-options")
public class FeedBackOptionsController {
}
package com.feedback.demo.controllers;
import com.feedback.demo.dto.FeedBackSettingsDTO;
import com.feedback.demo.dto.FeedBackSettingsRequest;
import com.feedback.demo.entities.FeedBackSettings;
import com.feedback.demo.service.FeedBackSettingsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
......@@ -25,7 +23,10 @@ public class FeedBackSettingsController {
private FeedBackSettingsService feedBackSettingsService;
@Operation(description = "Update FeedBack settings", responses = {
@ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = FeedBackSettingsRequest.class))), responseCode = "200")
@ApiResponse(content = @Content(
array = @ArraySchema(schema = @Schema(implementation = FeedBackSettingsRequest.class))),
responseCode = "200"
)
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Thành công"),
......@@ -34,11 +35,16 @@ public class FeedBackSettingsController {
@ApiResponse(responseCode = "403", description = "Truy cập bị cấm"),
})
@PostMapping("/update")
public ResponseEntity<FeedBackSettingsRequest> addFeedbackSettings(@Valid @RequestBody FeedBackSettingsRequest fsRequest){
public ResponseEntity<FeedBackSettingsRequest> addFeedbackSettings(
@Valid @RequestBody FeedBackSettingsRequest fsRequest
){
return ResponseEntity.ok(feedBackSettingsService.updateSettings(fsRequest));
}
@Operation(description = "Get FeedBack settings", responses = {
@ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = FeedBackSettings.class))), responseCode = "200")
@ApiResponse(content = @Content
(array = @ArraySchema(schema = @Schema(implementation = FeedBackSettingsRequest.class))),
responseCode = "200"
)
})
@GetMapping("/")
public ResponseEntity<FeedBackSettingsRequest> getFeedbackSettings(){
......
package com.feedback.demo.controllers;
import com.feedback.demo.dto.ChangePasswordRequest;
import com.feedback.demo.dto.LoginRequest;
import com.feedback.demo.dto.LoginResponse;
import com.feedback.demo.dto.SignupRequest;
import com.feedback.demo.entities.FeedBackSettings;
import com.feedback.demo.dto.login.ChangePasswordRequest;
import com.feedback.demo.dto.login.LoginRequest;
import com.feedback.demo.dto.login.LoginResponse;
import com.feedback.demo.dto.login.SignupRequest;
import com.feedback.demo.repositories.UserRepository;
import com.feedback.demo.service.JwtService;
import com.feedback.demo.service.UserService;
......@@ -32,8 +31,11 @@ public class UserController {
@Autowired
JwtService jwtService;
@Operation(description = "Update FeedBack settings", responses = {
@ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = FeedBackSettings.class))), responseCode = "200")
@Operation(description = "Login", responses = {
@ApiResponse(content = @Content(
array = @ArraySchema(schema = @Schema(implementation = LoginRequest.class))),
responseCode = "200"
)
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Thành công"),
......@@ -46,17 +48,37 @@ public class UserController {
return userService.getLoginResponse(loginRequest.getUsername(), loginRequest.getPassword());
}
@Operation(description = "Change Password", responses = {
@ApiResponse(content = @Content(
array = @ArraySchema(schema = @Schema(implementation = ChangePasswordRequest.class))),
responseCode = "200"
)
})
@PostMapping("/auth/changepass")
public LoginResponse changePassword(@Valid @RequestBody ChangePasswordRequest changePasswordRequest) {
public LoginResponse changePassword(
@Valid @RequestBody ChangePasswordRequest changePasswordRequest
) {
return userService.changePassword(changePasswordRequest);
}
@Operation(description = "Sign up", responses = {
@ApiResponse(content = @Content(
array = @ArraySchema(schema = @Schema(implementation = SignupRequest.class))),
responseCode = "200"
)
})
@PostMapping("/auth/signup")
public LoginResponse signup(@Valid @RequestBody SignupRequest signupRequest) throws Exception {
System.out.println(signupRequest);
return userService.signUp(signupRequest);
}
@Operation(description = "Validate token", responses = {
@ApiResponse(content = @Content(
array = @ArraySchema(schema = @Schema(implementation = String.class))),
responseCode = "200"
)
})
@GetMapping("/auth/verify/{token}")
public LoginResponse checkToken(@PathVariable String token) throws IOException {
return userService.checkToken(token);
......
package com.feedback.demo.dto;
package com.feedback.demo.dto.login;
import lombok.AllArgsConstructor;
import lombok.Builder;
......
package com.feedback.demo.dto;
package com.feedback.demo.dto.login;
import lombok.AllArgsConstructor;
import lombok.Builder;
......
package com.feedback.demo.dto;
package com.feedback.demo.dto.login;
import lombok.AllArgsConstructor;
import lombok.Builder;
......
package com.feedback.demo.dto;
package com.feedback.demo.dto.login;
import lombok.AllArgsConstructor;
import lombok.Builder;
......
package com.feedback.demo.exceptionhandle;
public class FeedBackNotFoundException extends RuntimeException{
private static final long serialVersionUID = 1L;
public FeedBackNotFoundException() {
super("Feed Back Not Found ! FeedBack id not exits !");
}
}
package com.feedback.demo.exceptionhandle;
public class PageNotFoundException extends RuntimeException{
private static final long serialVersionUID = 1L;
public PageNotFoundException() {
super("Page Not Found !");
}
}
package com.feedback.demo.mappers;
import com.feedback.demo.dto.FeedBackDTO;
import com.feedback.demo.entities.FeedBack;
public class FeedBackMapper {
public static FeedBackDTO toDTO(FeedBack feedBack){
return FeedBackDTO.builder()
.id(feedBack.getId())
.fullName(feedBack.getFullName())
.appName(feedBack.getAppName())
.email(feedBack.getEmail())
.phone(feedBack.getPhone())
.type(feedBack.getType())
.content(feedBack.getContent())
.imageUrl(feedBack.getImageUrl())
.url(feedBack.getUrl())
.createDate(feedBack.getCreateDate())
.updateDate(feedBack.getUpdateDate())
.build();
}
public static FeedBack toEntity(FeedBackDTO feedBackDTO){
return FeedBack.builder()
.id(feedBackDTO.getId())
.fullName(feedBackDTO.getFullName())
.appName(feedBackDTO.getAppName())
.email(feedBackDTO.getEmail())
.phone(feedBackDTO.getPhone())
.type(feedBackDTO.getType())
.content(feedBackDTO.getContent())
.imageUrl(feedBackDTO.getImageUrl())
.url(feedBackDTO.getUrl())
.createDate(feedBackDTO.getCreateDate())
.updateDate(feedBackDTO.getUpdateDate())
.build();
}
}
package com.feedback.demo.service;
import com.feedback.demo.dto.FeedBackOptionsDTO;
import com.feedback.demo.entities.FeedBackOptions;
import com.feedback.demo.entities.User;
import com.feedback.demo.repositories.FeedBackOptionsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class FeedBackOptionsService {
@Autowired
private FeedBackOptionsRepository feedBackOptionsRepository;
@Autowired
private UserService userService;
// public FeedBackOptionsDTO updateAllFeedbackOptions(FeedBackOptionsDTO feedBackOptionsDTO){
// User currentUser = userService.getCurrentUser();
// List<FeedBackOptions> allOptions = feedBackOptionsRepository.findAllByUser(currentUser.getId());
// if (allOptions!=null){
//
// }
// }
}
package com.feedback.demo.service;
import com.feedback.demo.dto.FeedBackDTO;
import com.feedback.demo.entities.FeedBack;
import com.feedback.demo.exceptionhandle.FeedBackNotFoundException;
import com.feedback.demo.exceptionhandle.PageNotFoundException;
import com.feedback.demo.mappers.FeedBackMapper;
import com.feedback.demo.repositories.FeedBackRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class FeedBackService {
@Autowired
private FeedBackRepository feedBackRepository;
@Value("${page.size:10}")
private Integer pageSize;
@Value("${sort.default:createDate}")
private String sortBy;
public List<FeedBackDTO> getFeedback(String pageIndex) {
try {
int page = Integer.parseInt(pageIndex);
Page<FeedBack> feedBackPage = feedBackRepository
.findAll(PageRequest.of(page, pageSize, Sort.by(sortBy)));
if (page > feedBackPage.getTotalPages()) {
throw new PageNotFoundException();
}
return feedBackPage.stream()
.map(FeedBackMapper::toDTO)
.collect(Collectors.toList());
} catch (NumberFormatException e) {
log.error(e.getMessage());
throw new FeedBackNotFoundException();
}
}
public FeedBackDTO getFeedBackById(String id) {
try {
Long feedBackId = Long.parseLong(id);
return feedBackRepository
.findById(feedBackId)
.map(FeedBackMapper::toDTO)
.orElseThrow(FeedBackNotFoundException::new);
} catch (NumberFormatException e) {
log.error(e.getMessage());
throw new FeedBackNotFoundException();
}
}
}
package com.feedback.demo.service;
import com.feedback.demo.config.CustomUserDetail;
import com.feedback.demo.dto.ChangePasswordRequest;
import com.feedback.demo.dto.LoginResponse;
import com.feedback.demo.dto.SignupRequest;
import com.feedback.demo.dto.login.ChangePasswordRequest;
import com.feedback.demo.dto.login.LoginResponse;
import com.feedback.demo.dto.login.SignupRequest;
import com.feedback.demo.entities.FeedBackOptions;
import com.feedback.demo.entities.FeedBackSettings;
import com.feedback.demo.entities.User;
......@@ -22,9 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
@Service
@Transactional(rollbackFor = Exception.class)
......
......@@ -7,3 +7,7 @@ spring.datasource.password=123456
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jackson.serialization.fail-on-empty-beans=false
#------
page.size = 10
sort.default = createDate
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment