Skip to content
Snippets Groups Projects
Commit 05489712 authored by Martin Gargalovič's avatar Martin Gargalovič
Browse files

added OpenApi and facade to controller

parent 0fa08883
No related branches found
No related tags found
3 merge requests!31M2,!28M2 user,!27Draft: M2 user
package org.fuseri.modulelanguageschool.user;
import io.swagger.v3.oas.annotations.Operation;
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.responses.ApiResponses;
import jakarta.persistence.EntityNotFoundException;
import org.fuseri.model.dto.common.Result;
import org.fuseri.model.dto.user.AddressDto;
import org.fuseri.model.dto.user.UserAddLanguageDto;
import org.fuseri.model.dto.user.UserCreateDto;
import org.fuseri.model.dto.user.UserDto;
import org.fuseri.model.dto.user.UserLoginDto;
import org.fuseri.modulelanguageschool.course.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import jakarta.validation.constraints.PositiveOrZero;
......@@ -20,71 +27,120 @@ import java.util.List;
@RequestMapping("/users")
public class UserController {
private final UserService service;
private final UserMapper mapper;
private final UserFacade facade;
@Autowired
public UserController(UserService service/*, UserMapper mapper*/) {
this.service = service;
this.mapper = null;
public UserController(UserFacade facade) {
this.facade = facade;
}
@Operation(summary = "Get a user by Id", description = "Returns a user with specified Id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User with the specified Id is retrieved Successfuly",
content = @Content(schema = @Schema(implementation = UserDto.class)
)),
@ApiResponse(responseCode = "404", description = "User with the specified ID was not found.")
})
@GetMapping("/{id}")
public UserDto find(@PathVariable String id) {
return new UserDto("spracher","spracher@gmail.com","Sprach","MeNot",new AddressDto());
public ResponseEntity<UserDto> find(@PathVariable Long id) {
try {
return ResponseEntity.ok(facade.find(id));
} catch (EntityNotFoundException e) {
return ResponseEntity.notFound().build();
}
}
@Operation(summary = "Create a User", description = "Creates a new User.")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "User created successfully."),
@ApiResponse(responseCode = "400", description = "Invalid input.")
})
@PostMapping
public UserDto create(@Valid @RequestBody UserCreateDto dto) {
return new UserDto(dto.getUsername(),dto.getEmail(),dto.getFirstName(),dto.getLastName(),dto.getAddress());
public ResponseEntity<UserDto> create(@Valid @RequestBody UserCreateDto dto) {
UserDto user = facade.create(dto);
return ResponseEntity.status(HttpStatus.CREATED).body(user);
}
@Operation(summary = "Delete a User with specified ID", description = "Deletes a User with the specified ID.")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "User with the specified ID deleted successfully."),
})
@DeleteMapping("/{id}")
public UserDto deleteUser(@PathVariable String id) {
return new UserDto("spracher","spracher@gmail.com","Sprach","MeNot",new AddressDto());
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
facade.delete(id);
return ResponseEntity.noContent().build();
}
@Operation(summary = "Update a User", description = "Updates a User with the specified ID.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User with the specified ID updated successfully."),
@ApiResponse(responseCode = "400", description = "Invalid input."),
@ApiResponse(responseCode = "404", description = "User with the specified ID was not found.")
})
@PutMapping("/update/{id}")
public UserDto update(@PositiveOrZero @PathVariable String id,@Valid @RequestBody UserCreateDto user) {
public ResponseEntity<UserDto> update(@PositiveOrZero @PathVariable Long id, @Valid @RequestBody UserCreateDto dto) {
return new UserDto(user.getUsername(),user.getEmail(),user.getFirstName(),user.getLastName(),user.getAddress());
try {
return ResponseEntity.ok(facade.update(id, dto));
} catch (EntityNotFoundException e) {
return ResponseEntity.notFound().build();
}
}
@Operation(summary = "Get Users in paginated format", description = "Returns Users in paginated format.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully retrieved paginated Users"),
@ApiResponse(responseCode = "400", description = "Invalid page number supplied"),
})
@GetMapping("/all")
public Result<UserDto> findAll(@PositiveOrZero @RequestParam int page) {
var res = new Result<UserDto>();
res.setItems(List.of(new UserDto("spracher","spracher@gmail.com","Sprach","MeNot",new AddressDto())
));
return res;
public ResponseEntity<Result<UserDto>> findAll(@PositiveOrZero @RequestParam int page) {
return ResponseEntity.ok(facade.findAll(page));
}
//TODO: add authentication
@PostMapping("/login")
public String login(@Valid @RequestBody UserLoginDto dto) {
public String login(@RequestBody UserLoginDto dto) {
return String.format("User %s has spawned", dto.getUsername());
}
//TODO: add authentication
@PostMapping("/logout/{id}")
public String logout(@PathVariable String id) {
public String logout(@PathVariable Long id) {
return "user has logged out";
}
// TODO do according to Course
@GetMapping("/finished/{id}")
public List<Course> getFinished(@PathVariable String id) {
public List<Course> getFinished(@PathVariable Long id) {
return new ArrayList<>();
}
// TODO do according to Course
@GetMapping("/enrolled/{id}")
public List<Course> getEnrolled(@PathVariable String id) {
public List<Course> getEnrolled(@PathVariable Long id) {
return new ArrayList<>();
}
@Operation(summary = "adds a language", description = "adds a new language and proficiency to user")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully added a language"),
@ApiResponse(responseCode = "404", description = "User with given Id does not exist"),
@ApiResponse(responseCode = "400", description = "Invalid input")
})
@PutMapping("/addLanguage/{id}")
public String addLanguage(@PathVariable String id,@Valid @RequestBody UserAddLanguageDto body) {
return "added Absolutely Nothing successfully!";
public ResponseEntity<UserDto> addLanguage(@PathVariable Long id, @Valid @RequestBody UserAddLanguageDto body) {
try {
return ResponseEntity.ok(facade.addLanguage(id, body));
} catch (EntityNotFoundException e) {
return ResponseEntity.notFound().build();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment