Skip to content

Commit 0c7c98b

Browse files
authored
Merge pull request #19 from AET-DevOps25/feat/user-service
Feat/user service
2 parents 5f1993e + 7fbe399 commit 0c7c98b

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package de.tum.userservice.profile;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
import java.util.List;
8+
9+
@RestController
10+
@RequestMapping("/api/v1/profiles")
11+
public class ProfileController {
12+
private final ProfileService service;
13+
14+
@Autowired
15+
public ProfileController(ProfileService service) {
16+
this.service = service;
17+
}
18+
19+
/**
20+
* POST /api/v1/profiles
21+
* Creates a new profile.
22+
*/
23+
@PostMapping
24+
public ResponseEntity<ProfileEntity> create(@RequestBody ProfileEntity profile) {
25+
ProfileEntity created = service.createProfile(profile);
26+
return ResponseEntity.ok(created);
27+
}
28+
29+
/**
30+
* GET /api/v1/profiles/{id}
31+
* Retrieves a profile by ID.
32+
*/
33+
@GetMapping("/{id}")
34+
public ResponseEntity<ProfileEntity> getById(@PathVariable Long id) {
35+
ProfileEntity profile = service.getProfileById(id);
36+
if (profile == null) {
37+
return ResponseEntity.notFound().build();
38+
} else {
39+
return ResponseEntity.ok(profile);
40+
}
41+
}
42+
43+
/**
44+
* PUT /api/v1/profiles/{id}
45+
* Updates a profile by ID.
46+
*/
47+
@PutMapping("/{id}")
48+
public ResponseEntity<ProfileEntity> update(@PathVariable Long id, @RequestBody ProfileEntity profile) {
49+
try {
50+
ProfileEntity updated = service.updateProfile(id, profile);
51+
return ResponseEntity.ok(updated);
52+
} catch (RuntimeException e) {
53+
return ResponseEntity.notFound().build();
54+
}
55+
}
56+
57+
/**
58+
* DELETE /api/v1/profiles/{id}
59+
* Deletes a profile by ID.
60+
*/
61+
@DeleteMapping("/{id}")
62+
public ResponseEntity<Void> delete(@PathVariable Long id) {
63+
service.deleteProfile(id);
64+
return ResponseEntity.noContent().build();
65+
}
66+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.tum.userservice.profile;
2+
3+
import jakarta.persistence.*;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
9+
@Entity
10+
@Table(name = "user_profile")
11+
@Getter
12+
@Setter
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
16+
public class ProfileEntity {
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.IDENTITY)
19+
private Long id;
20+
21+
@Column(nullable = false, unique = true)
22+
private String email;
23+
24+
private String firstName;
25+
private String lastName;
26+
27+
private String profilePicture;
28+
29+
// TODO: Replace with a more complex type if needed
30+
private String preference;
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.tum.userservice.profile;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface ProfileRepository extends JpaRepository<ProfileEntity, Long> {
6+
ProfileEntity findByEmail(String email);
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package de.tum.userservice.profile;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
6+
public interface ProfileService {
7+
ProfileEntity createProfile(ProfileEntity profile);
8+
ProfileEntity getProfileById(Long id);
9+
ProfileEntity updateProfile(Long id, ProfileEntity profile);
10+
void deleteProfile(Long id);
11+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package de.tum.userservice.profile;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
import org.springframework.transaction.annotation.Transactional;
6+
7+
import java.util.List;
8+
import java.util.Optional;
9+
10+
@Service
11+
@Transactional
12+
public class ProfileServiceImpl implements ProfileService{
13+
private final ProfileRepository repository;
14+
15+
@Autowired
16+
public ProfileServiceImpl(ProfileRepository repository) {
17+
this.repository = repository;
18+
}
19+
20+
@Override
21+
public ProfileEntity createProfile(ProfileEntity profile) {
22+
return repository.save(profile);
23+
}
24+
25+
@Override
26+
public ProfileEntity getProfileById(Long id) {
27+
return repository.findById(id).orElse(null);
28+
}
29+
30+
@Override
31+
public ProfileEntity updateProfile(Long id, ProfileEntity profile) {
32+
return repository.findById(id)
33+
.map(existing -> {
34+
existing.setFirstName(profile.getFirstName());
35+
existing.setLastName(profile.getLastName());
36+
existing.setEmail(profile.getEmail());
37+
return repository.save(existing);
38+
})
39+
.orElseThrow(() -> new RuntimeException("Profile not found with id " + id));
40+
}
41+
42+
@Override
43+
public void deleteProfile(Long id) {
44+
repository.deleteById(id);
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
spring:
2+
application:
3+
name: UserService
4+
5+
datasource:
6+
url: jdbc:postgresql://localhost:5432/team-drop-database
7+
username: user
8+
password: password
9+
driver-class-name: org.postgresql.Driver
10+
11+
jpa:
12+
hibernate:
13+
ddl-auto: update
14+
show-sql: false
15+
properties:
16+
hibernate:
17+
format_sql: true
18+
database: postgresql
19+
database-platform: org.hibernate.dialect.PostgreSQLDialect

0 commit comments

Comments
 (0)