Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: controller use composed annotation #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ public class AccountController {
private AccountService accountService;

@PreAuthorize("#oauth2.hasScope('server') or #name.equals('demo')")
@RequestMapping(path = "/{name}", method = RequestMethod.GET)
@GetMapping("/{name}")
public Account getAccountByName(@PathVariable String name) {
return accountService.findByName(name);
}

@RequestMapping(path = "/current", method = RequestMethod.GET)
@GetMapping("/current")
public Account getCurrentAccount(Principal principal) {
return accountService.findByName(principal.getName());
}

@RequestMapping(path = "/current", method = RequestMethod.PUT)
@PutMapping("/current")
public void saveCurrentAccount(Principal principal, @Valid @RequestBody Account account) {
accountService.saveChanges(principal.getName(), account);
}

@RequestMapping(path = "/", method = RequestMethod.POST)
@PostMapping("/")
public Account createNewAccount(@Valid @RequestBody User user) {
return accountService.create(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.piggymetrics.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its's a bad practice to use asterisk imports, please
if you use intellij: https://stackoverflow.com/questions/3348816/intellij-never-use-wildcard-imports


import javax.validation.Valid;
import java.security.Principal;
Expand All @@ -19,13 +16,13 @@ public class UserController {
@Autowired
private UserService userService;

@RequestMapping(value = "/current", method = RequestMethod.GET)
@GetMapping("/current")
public Principal getUser(Principal principal) {
return principal;
}

@PreAuthorize("#oauth2.hasScope('server')")
@RequestMapping(method = RequestMethod.POST)
@PostMapping
public void createUser(@Valid @RequestBody User user) {
userService.create(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.piggymetrics.notification.domain.Recipient;
import com.piggymetrics.notification.service.RecipientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
Expand All @@ -18,12 +15,12 @@ public class RecipientController {
@Autowired
private RecipientService recipientService;

@RequestMapping(path = "/current", method = RequestMethod.GET)
@GetMapping("/current")
public Object getCurrentNotificationsSettings(Principal principal) {
return recipientService.findByAccountName(principal.getName());
}

@RequestMapping(path = "/current", method = RequestMethod.PUT)
@PutMapping("/current")
public Object saveCurrentNotificationsSettings(Principal principal, @Valid @RequestBody Recipient recipient) {
return recipientService.save(principal.getName(), recipient);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public class StatisticsController {
@Autowired
private StatisticsService statisticsService;

@RequestMapping(value = "/current", method = RequestMethod.GET)
@GetMapping("/current")
public List<DataPoint> getCurrentAccountStatistics(Principal principal) {
return statisticsService.findByAccountName(principal.getName());
}

@PreAuthorize("#oauth2.hasScope('server') or #accountName.equals('demo')")
@RequestMapping(value = "/{accountName}", method = RequestMethod.GET)
@GetMapping("/{accountName}")
public List<DataPoint> getStatisticsByAccountName(@PathVariable String accountName) {
return statisticsService.findByAccountName(accountName);
}

@PreAuthorize("#oauth2.hasScope('server')")
@RequestMapping(value = "/{accountName}", method = RequestMethod.PUT)
@PutMapping("/{accountName}")
public void saveAccountStatistics(@PathVariable String accountName, @Valid @RequestBody Account account) {
statisticsService.save(accountName, account);
}
Expand Down