Skip to content

Commit

Permalink
fixed add button handling and autoclosing of add popovers.
Browse files Browse the repository at this point in the history
improved error handling
  • Loading branch information
Daniel Prange committed Aug 22, 2022
1 parent d3c9788 commit 4fbd5ac
Show file tree
Hide file tree
Showing 16 changed files with 37,579 additions and 35,659 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.security.Principal;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -40,16 +42,21 @@ class BotController {

@PostMapping("/bots/")
public ResponseEntity<?> addBot(@RequestBody TargetBotForm form, Principal principal) {
User user = userService.findUserByName(principal.getName());
Server server = serverService.findById(form.getServerId());
Channel channel = channelService.findById(form.getChannelId());
Bot bot = new Bot(server, channel, form.getName(), form.getPattern(), form.getMaxParallelDownloads());
botService.save(bot);
user.getBots().add(bot);
userService.saveUser(user);

eventService.publishEvent(SocketEvents.NEW_SERVER, bot);
return new ResponseEntity("Bot added succcessfully", HttpStatus.OK);
try {
User user = userService.findUserByName(principal.getName());
Server server = serverService.findById(form.getServerId());
Channel channel = channelService.findById(form.getChannelId());
Bot bot = new Bot(server, channel, form.getName(), form.getPattern(), form.getMaxParallelDownloads());
botService.save(bot);
user.getBots().add(bot);
userService.saveUser(user);
eventService.publishEvent(SocketEvents.NEW_SERVER, bot);
return new ResponseEntity<>("Bot added successfully", HttpStatus.OK);
} catch (ConstraintViolationException e) {
log.error(e.getMessage());
String errormessage = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining("\n"));
return new ResponseEntity<>(errormessage, HttpStatus.BAD_REQUEST);
}
}

/**
Expand All @@ -59,15 +66,15 @@ public ResponseEntity<?> addBot(@RequestBody TargetBotForm form, Principal princ
public ResponseEntity<List<BotTO>> getAllBots(Principal principal) {
User user = userService.findUserByName(principal.getName());
List<BotTO> bots = user.getBots().stream().map(BotTO::new).collect(Collectors.toList());
return new ResponseEntity(bots, HttpStatus.OK);
return new ResponseEntity<>(bots, HttpStatus.OK);
}

/**
* @return an example bot object to populate the attributes for bot creation popover
*/
@GetMapping("/bots/example")
public ResponseEntity<List<Bot>> getExampleBot(Principal principal) {
return new ResponseEntity(List.of(new Bot()), HttpStatus.OK);
return new ResponseEntity<>(List.of(new Bot()), HttpStatus.OK);
}

@DeleteMapping("/bots/")
Expand All @@ -77,9 +84,9 @@ public ResponseEntity<?> delete( Long botId, Principal principal) {
currentUser.getBots().remove(botService.findById(botId));
userService.saveUser(currentUser);
botService.delete(botId);
return new ResponseEntity("Bot deleted succcessfully.", HttpStatus.OK);
return new ResponseEntity<>("Bot deleted successfully.", HttpStatus.OK);
}catch (Exception e) {
return new ResponseEntity("Bot could not be deleted", HttpStatus.CONFLICT);
return new ResponseEntity<>("Bot could not be deleted", HttpStatus.CONFLICT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.security.Principal;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -31,30 +32,35 @@ class ChannelController {
@GetMapping("/channels/")
public ResponseEntity<List<ChannelTO>> getAllChannels() {
List<ChannelTO> channels = channelService.list().stream().map(ChannelTO::new).collect(Collectors.toList());
return new ResponseEntity(channels, HttpStatus.OK);
return new ResponseEntity<>(channels, HttpStatus.OK);
}

@PostMapping("/channels/")
public ResponseEntity<?> addChannel(@RequestBody ChannelForm channelForm) {
Channel channel = channelService.save(new Channel(channelForm.getName()));
return new ResponseEntity("Download added succcessfully. id=[" + channel.getId() + "]", HttpStatus.OK);
try {
Channel channel = channelService.save(new Channel(channelForm.getName()));
return new ResponseEntity<>("Download added successfully. id=[" + channel.getId() + "]", HttpStatus.OK);
} catch (ConstraintViolationException e) {
log.error(e.getMessage());
return new ResponseEntity<>(e.getConstraintViolations().stream().findFirst().get().getMessage(), HttpStatus.BAD_REQUEST);
}
}

/**
* @return an example channel object to populate the attributes for channel creation popover
*/
@GetMapping("/channels/example")
public ResponseEntity<List<Channel>> getExampleChannel(Principal principal) {
return new ResponseEntity(List.of(new Channel()), HttpStatus.OK);
return new ResponseEntity<>(List.of(new Channel()), HttpStatus.OK);
}

@DeleteMapping("/channels/")
public ResponseEntity<?> delete( Long channelId) {
try {
channelService.delete(channelId);
return new ResponseEntity("Channel deleted succcessfully.", HttpStatus.OK);
return new ResponseEntity<>("Channel deleted successfully.", HttpStatus.OK);
}catch (Exception e) {
return new ResponseEntity("Channel could not be deleted", HttpStatus.CONFLICT);
return new ResponseEntity<>("Channel could not be deleted", HttpStatus.CONFLICT);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.security.Principal;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -37,8 +40,14 @@ public ResponseEntity<List<ServerTO>> getAllServers() {

@PostMapping("/servers/")
public ResponseEntity<?> addServer(@RequestBody ServerForm serverForm) {
Server server = serverService.save(new Server(serverForm.getName(), serverForm.getServerUrl()));
return new ResponseEntity("Download added succcessfully. id=[" + server.getId() + "]", HttpStatus.OK);
try {
Server server = serverService.save(new Server(serverForm.getName(), serverForm.getServerUrl()));
return new ResponseEntity("Download added successfully. id=[" + server.getId() + "]", HttpStatus.OK);
} catch (ConstraintViolationException e) {
log.error(e.getMessage());
String errormessage = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining("\n"));
return new ResponseEntity(errormessage, HttpStatus.BAD_REQUEST);
}
}

/**
Expand All @@ -53,7 +62,7 @@ public ResponseEntity<List<Server>> getExampleServer(Principal principal) {
public ResponseEntity<?> delete( Long serverId) {
try {
serverService.delete(serverId);
return new ResponseEntity("Server deleted succcessfully.", HttpStatus.OK);
return new ResponseEntity("Server deleted successfully.", HttpStatus.OK);
}catch (Exception e) {
return new ResponseEntity("Server could not be deleted", HttpStatus.CONFLICT);
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/de/maggiwuerze/xdccloader/model/entity/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import lombok.Data;

@Entity
Expand All @@ -26,15 +28,19 @@ public class Bot {
@JoinColumn(name = "SERVER_ID")
Server server;

@Size(min=1, message = "Bot name must be at least 1 character long")
@Column(nullable = false)
String name;

@Size(min=1, message = "Bot pattern must be at least 1 character long")
@Column(nullable = false)
String pattern;

@Column(nullable = false)
LocalDateTime creationDate = LocalDateTime.now();


@Min(1L)
@Column(nullable = false)
Long maxParallelDownloads = 3L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.Size;

@Entity
public class Channel {
Expand All @@ -14,6 +15,7 @@ public class Channel {
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@Size(min=1, message = "Channel name must be at least 1 character long")
@Column(nullable = false)
String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.Size;

@Entity
public class Server {
Expand All @@ -14,9 +15,11 @@ public class Server {
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@Size(min=1, message = "Server name must be at least 1 character long")
@Column(nullable = false)
String name;

@Size(min=1, message = "Server URL must be at least 1 character long")
@Column(nullable = false)
String serverUrl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.validation.constraints.Size;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand All @@ -32,9 +33,11 @@ public class User implements UserDetails {
@Column(nullable = false)
Boolean active = true;

@Size(min=1)
@Column(nullable = false, unique = true)
String name;

@Size(min=1)
@Column(nullable = false, unique = true)
String password;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import lombok.Data;
import lombok.NoArgsConstructor;

Expand All @@ -31,16 +33,19 @@ public class UserSettings {
@Column(nullable = false)
LocalDateTime creationDate = LocalDateTime.now();

@Min(value = 1L, message = "Refresh rate may not be shorter than 1 second")
@Column(nullable = false)
Long refreshrateInSeconds = 1L;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
DownloadSort downloadSortBy = DownloadSort.PROGRESS;

@Min(value = 1, message = "Session Timeout may not be shorter than 1 second")
@Column(nullable = false)
Long sessionTimeout = 300L;

@Size(min=1, message = "Download path may not be empty")
@Column(nullable = false)
String downloadPath = "xdcc/";

Expand Down
Loading

0 comments on commit 4fbd5ac

Please sign in to comment.