Skip to content

Commit

Permalink
DEAR-112: add update user
Browse files Browse the repository at this point in the history
  • Loading branch information
baurnick committed Jul 13, 2024
1 parent fb50b6b commit 52031db
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package ch.fhnw.deardevbackend.controller;

import ch.fhnw.deardevbackend.dto.UserAndProviderDTO;
import ch.fhnw.deardevbackend.dto.UserDTO;
import ch.fhnw.deardevbackend.entities.User;
import ch.fhnw.deardevbackend.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -33,4 +31,9 @@ public ResponseEntity<User> getUserById(@PathVariable Integer id) {
public ResponseEntity<UserAndProviderDTO> getUserByProviderById(@PathVariable Integer id) {
return ResponseEntity.ok().body(userService.getUserWithProviderById(id));
}

@PutMapping("user/update")
public void updateUser(@RequestBody UserDTO request) {
userService.updateUser(request.getId(), request.getUsername());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public class UserAndProviderDTO {
private String name;
private String email;
private String provider;
private String username;
}
9 changes: 9 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/dto/UserDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ch.fhnw.deardevbackend.dto;

import lombok.Data;

@Data
public class UserDTO {
private int id;
private String username;
}
1 change: 1 addition & 0 deletions src/main/java/ch/fhnw/deardevbackend/entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class User {
@Id
private int id;
private String name;
private String username;
private String email;
private String image;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface UserProviderMapper {
@Mapping(source = "user.id", target = "id")
@Mapping(source = "user.name", target = "name")
@Mapping(source = "user.email", target = "email")
@Mapping(source = "user.username", target = "username")
@Mapping(source = "provider", target = "provider")
UserAndProviderDTO toDto(User user, String provider);
}
12 changes: 12 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springdoc.api.OpenApiResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -37,6 +38,17 @@ public UserAndProviderDTO getUserWithProviderById(Integer id) {
return userMapper.toDto(user, provider);
}

@Transactional
public void updateUser(int id, String username) {
User user = userRepository.findById(id).orElseThrow(() -> new OpenApiResourceNotFoundException("User not found"));
if (user != null) {
user.setUsername(username);
userRepository.save(user);
} else {
throw new IllegalArgumentException("User not found");
}
}

// used for JWT filter
public Optional<User> findUserByEmail(String email) {
return userRepository.findByEmail(email);
Expand Down

0 comments on commit 52031db

Please sign in to comment.