|
| 1 | +package com.codedifferently.watertrackerapi.domain.userProfiles.controllers; |
| 2 | + |
| 3 | +import com.codedifferently.watertrackerapi.domain.userProfiles.dtos.UserProfileCreateRequest; |
| 4 | +import com.codedifferently.watertrackerapi.domain.userProfiles.dtos.UserProfileDTO; |
| 5 | +import com.codedifferently.watertrackerapi.domain.userProfiles.models.UserProfile; |
| 6 | +import com.codedifferently.watertrackerapi.domain.userProfiles.services.UserProfileService; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.web.bind.annotation.*; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +@Slf4j |
| 15 | +@RestController |
| 16 | +@RequestMapping("/api/v1/userprofile") |
| 17 | +public class UserProfileController { |
| 18 | + |
| 19 | + private UserProfileService userProfileService; |
| 20 | + |
| 21 | + @Autowired |
| 22 | + public UserProfileController(UserProfileService userProfileService) { |
| 23 | + this.userProfileService = userProfileService; |
| 24 | + } |
| 25 | + |
| 26 | + @PostMapping("create") |
| 27 | + public ResponseEntity<UserProfileDTO> create (@RequestBody UserProfileCreateRequest userDetail){ |
| 28 | + UserProfileDTO userProfile = userProfileService.create(userDetail); |
| 29 | + return new ResponseEntity<>(userProfile, HttpStatus.CREATED); |
| 30 | + } |
| 31 | + |
| 32 | + @GetMapping |
| 33 | + public ResponseEntity<Iterable<UserProfileDTO>> getAll(){ |
| 34 | + Iterable<UserProfileDTO> userProfiles = userProfileService.getAll(); |
| 35 | + return new ResponseEntity<>(userProfiles, HttpStatus.OK); |
| 36 | + } |
| 37 | + |
| 38 | + @GetMapping("{id}") |
| 39 | + public ResponseEntity<UserProfileDTO> getById(@PathVariable("id") String id){ |
| 40 | + UserProfileDTO userProfile = userProfileService.getById(id); |
| 41 | + return new ResponseEntity<>(userProfile, HttpStatus.OK); |
| 42 | + } |
| 43 | + |
| 44 | + @GetMapping("/findByEmail/{email}") |
| 45 | + public ResponseEntity<UserProfileDTO> getByEmail(@PathVariable("email")String email){ |
| 46 | + UserProfileDTO userProfile = userProfileService.getByEmail(email); |
| 47 | + return new ResponseEntity<>(userProfile, HttpStatus.OK); |
| 48 | + } |
| 49 | + |
| 50 | + @PutMapping("{id}") |
| 51 | + public ResponseEntity<UserProfileDTO> update(@PathVariable("id")String id, @RequestBody UserProfile userProfile){ |
| 52 | + UserProfileDTO userProfileDTO = userProfileService.update(id, userProfile); |
| 53 | + return new ResponseEntity<>(userProfileDTO, HttpStatus.ACCEPTED); |
| 54 | + } |
| 55 | + |
| 56 | + @DeleteMapping("{id}") |
| 57 | + public ResponseEntity delete(@PathVariable("id") String id){ |
| 58 | + userProfileService.delete(id); |
| 59 | + return new ResponseEntity(HttpStatus.NO_CONTENT); |
| 60 | + } |
| 61 | + |
| 62 | + @GetMapping("{id}/followers") |
| 63 | + public ResponseEntity<Iterable<UserProfileDTO>> getFollowers(@PathVariable("id") String userId){ |
| 64 | + Iterable<UserProfileDTO> followers = userProfileService.getFollowers(userId); |
| 65 | + return new ResponseEntity<>(followers, HttpStatus.OK); |
| 66 | + } |
| 67 | + |
| 68 | + @PostMapping("follow") |
| 69 | + public ResponseEntity follow(@RequestBody Map<String,String> data){ |
| 70 | + String userId = data.get("userId"); |
| 71 | + String followerId = data.get("followerId"); |
| 72 | + userProfileService.follow(userId, followerId); |
| 73 | + return new ResponseEntity(HttpStatus.ACCEPTED); |
| 74 | + } |
| 75 | + |
| 76 | + @PostMapping("unfollow") |
| 77 | + public ResponseEntity unfollow(@RequestBody Map<String,String> data){ |
| 78 | + String userId = data.get("userId"); |
| 79 | + String followerId = data.get("followerId"); |
| 80 | + userProfileService.unfollow(userId, followerId); |
| 81 | + return new ResponseEntity(HttpStatus.ACCEPTED); |
| 82 | + } |
| 83 | +} |
0 commit comments