update fix

This commit is contained in:
2026-05-09 11:35:30 +08:00
parent 1828c37fca
commit ba8c1e3be3
15 changed files with 158 additions and 84 deletions
@@ -73,6 +73,9 @@ public class UserPostController extends BaseController {
public String getDiscussionsById(HttpServletRequest request, ModelMap map, @RequestParam("id") Integer id) {
logger.info(">>> getDiscussionsById{}", id);
// Atomically increment view count before loading the discussion
discussionsService.incrementViewCount(id);
Discussion discussion = discussionsService.findOne(id);
// 获取此主题下的评论
List<Post> posts = postsService.findOneBy(id);
@@ -112,6 +112,7 @@ public class UserSystemController extends BaseController {
map.put("pageTotal", total);
map.put("pageNum", pageNum);
map.put("pageTotalPages", allDiscussionsPage.getTotalPages());
map.put("pageNumAll", allDiscussionsPage.getTotalPages());
UserInfo user = getUserInfo(request);
if (user != null) {
map.put("username", user.getUsername());
@@ -1,11 +1,16 @@
package com.yaoyuan.jiscuss.controller.api;
import com.yaoyuan.jiscuss.dto.UserCreateRequest;
import com.yaoyuan.jiscuss.dto.UserMapper;
import com.yaoyuan.jiscuss.dto.UserResponse;
import com.yaoyuan.jiscuss.entity.User;
import com.yaoyuan.jiscuss.exception.BaseException;
import com.yaoyuan.jiscuss.response.ApiResponse;
import com.yaoyuan.jiscuss.response.ResponseCode;
import com.yaoyuan.jiscuss.service.IUsersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,46 +35,51 @@ public class RestUserController {
@Autowired
private IUsersService usersService;
@Autowired
private UserMapper userMapper;
@PostMapping("/user")
@Operation(summary = "新增用户")
public User save(@RequestBody User user) {
User saveUser = usersService.insert(user);
if (saveUser != null) {
return saveUser;
} else {
throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
public ApiResponse<UserResponse> save(@Valid @RequestBody UserCreateRequest request) {
User user = userMapper.fromCreateRequest(request);
User saved = usersService.insert(user);
if (saved != null) {
return ApiResponse.ok(userMapper.toResponse(saved));
}
throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
}
@DeleteMapping("/user/{id}")
@Operation(summary = "删除用户")
public Boolean delete(@PathVariable Integer id) {
public ApiResponse<Boolean> delete(@PathVariable Integer id) {
usersService.remove(id);
return true;
return ApiResponse.ok(true);
}
@PutMapping("/user/{id}")
@Operation(summary = "修改用户")
public User update(@RequestBody User user, @PathVariable Integer id) {
User updateuser = usersService.update(user, id);
if (updateuser != null) {
return updateuser;
} else {
throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
public ApiResponse<UserResponse> update(@RequestBody User user, @PathVariable Integer id) {
User updated = usersService.update(user, id);
if (updated != null) {
return ApiResponse.ok(userMapper.toResponse(updated));
}
throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
}
@GetMapping("/user/{id}")
@Operation(summary = "获取用户")
public User getUser(@PathVariable Integer id) {
public ApiResponse<UserResponse> getUser(@PathVariable Integer id) {
User user = usersService.findOne(id);
logger.info("获取用户==>{}", user);
return user;
return ApiResponse.ok(userMapper.toResponse(user));
}
@GetMapping("/user")
@Operation(summary = "获取全部用户")
public List<User> getAllUsers() {
return usersService.getAllList();
public ApiResponse<List<UserResponse>> getAllUsers() {
List<UserResponse> list = usersService.getAllList().stream()
.map(userMapper::toResponse)
.toList();
return ApiResponse.ok(list);
}
}
@@ -77,5 +77,8 @@ public class Discussion implements Serializable {
@Column(name = "create_time")
private Date createTime;
@Column(name = "view_count")
private Integer viewCount = 0;
}
@@ -1,5 +1,6 @@
package com.yaoyuan.jiscuss.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
@@ -39,7 +40,8 @@ public class User implements Serializable {
@Column(name = "email")
private String email;
/** BCrypt-hashed password. Column length must be >= 68 (see V2 migration). */
/** BCrypt-hashed password. Column length must be >= 68 (see V2 migration). Never serialized to JSON. */
@JsonIgnore
@Column(name = "password")
private String password;
@@ -77,10 +77,9 @@ public class UserInfo implements UserDetails {
@Override
public String toString() {
return "UserInfo{" +
"authorities=" + authorities +
", password='" + password + '\'' +
", username='" + username + '\'' +
"username='" + username + '\'' +
", id='" + id + '\'' +
", authorities=" + authorities +
'}';
}
}
@@ -4,8 +4,10 @@ import com.yaoyuan.jiscuss.entity.Discussion;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public interface DiscussionsRepository extends JpaRepository<Discussion, Integer> {
@@ -14,5 +16,8 @@ public interface DiscussionsRepository extends JpaRepository<Discussion, Integer
"SELECT discussion_id from discussiontag where tag_id = ?1 ) ", nativeQuery = true)
Page<Discussion> findByQuery(String tagId, Pageable pageable);
@Modifying
@Transactional
@Query("UPDATE Discussion d SET d.viewCount = d.viewCount + 1 WHERE d.id = :id")
void incrementViewCount(@org.springframework.data.repository.query.Param("id") Integer id);
}
@@ -14,4 +14,6 @@ public interface IDiscussionsService {
Discussion findOne(Integer id);
Page<Discussion> queryAllDiscussionsList(Discussion discussion, int pageNumNew, int pageSiz, String tag, String type);
void incrementViewCount(Integer id);
}
@@ -35,8 +35,7 @@ public class DiscussionsServiceImpl implements IDiscussionsService {
@Transactional(readOnly = true)
@Override
public Discussion findOne(Integer id) {
// getReferenceById replaces removed JpaRepository.getOne()
return discussionsRepository.getReferenceById(id);
return discussionsRepository.findById(id).orElse(null);
}
@Transactional(readOnly = true)
@@ -55,4 +54,10 @@ public class DiscussionsServiceImpl implements IDiscussionsService {
return discussionsRepository.findAll(example, pageable);
}
}
@Transactional
@Override
public void incrementViewCount(Integer id) {
discussionsRepository.incrementViewCount(id);
}
}