generated from l3r8yJ/elegant
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
h1alexbel
committed
Jul 7, 2023
1 parent
f4c08a8
commit 9bf0059
Showing
21 changed files
with
816 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/io/blamer/hub/controller/ChatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.blamer.hub.controller; | ||
|
||
import io.blamer.hub.model.Chats; | ||
import io.blamer.hub.pg.PgChat; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Chat Controller. | ||
* | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/chats") | ||
public class ChatController { | ||
|
||
/** | ||
* Chats. | ||
*/ | ||
private final Chats chats; | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param chats Chats | ||
*/ | ||
public ChatController(@Qualifier("validated") final Chats chats) { | ||
this.chats = chats; | ||
} | ||
|
||
/** | ||
* Register new Chat ID. | ||
* | ||
* @param chat Chat ID | ||
* @return Mono void publisher | ||
*/ | ||
@PostMapping | ||
public Mono<Void> registerChatId(@RequestBody final Long chat) { | ||
return this.chats.add(new PgChat(chat)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.blamer.hub.controller; | ||
|
||
import io.blamer.hub.rq.ChatAlreadyExists; | ||
import io.github.eocqrs.eokson.Jocument; | ||
import io.github.eocqrs.eokson.MutableJson; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
/** | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
@Slf4j | ||
@RestControllerAdvice | ||
public class HubAdvice { | ||
|
||
/** | ||
* Handle {@link ChatAlreadyExists}. | ||
* | ||
* @param ex ChatAlreadyExists | ||
* @return ResponseEntity | ||
*/ | ||
@ExceptionHandler(ChatAlreadyExists.class) | ||
public ResponseEntity<byte[]> handle(final ChatAlreadyExists ex) { | ||
log.debug("Chat ID already exists", ex); | ||
return new ResponseEntity<>( | ||
new Jocument( | ||
new MutableJson() | ||
.with("code", HttpStatus.CONFLICT.value()) | ||
.with("message", ex.getMessage()) | ||
).byteArray(), | ||
HttpStatus.CONFLICT | ||
); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/io/blamer/hub/controller/TokenController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.blamer.hub.controller; | ||
|
||
import io.blamer.hub.model.Tokens; | ||
import io.blamer.hub.rq.RequestToken; | ||
import io.blamer.hub.rq.TokenToAdd; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Token controller. | ||
* | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/tokens") | ||
public class TokenController { | ||
|
||
/** | ||
* Tokens. | ||
*/ | ||
private final Tokens tokens; | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param tkns Tokens | ||
*/ | ||
public TokenController(final Tokens tkns) { | ||
this.tokens = tkns; | ||
} | ||
|
||
/** | ||
* Registers new Token with Chat ID. | ||
* | ||
* @param request RequestToken | ||
* @return Mono void publisher | ||
* @throws Exception if something went wrong | ||
*/ | ||
@PostMapping | ||
Mono<Void> registerToken(@RequestBody final RequestToken request) | ||
throws Exception { | ||
return this.tokens.add(new TokenToAdd(request).value()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Endpoints. | ||
* | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
package io.blamer.hub.controller; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.blamer.hub.model; | ||
|
||
/** | ||
* Chat. | ||
* | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
public interface Chat { | ||
|
||
/** | ||
* Chat ID. | ||
* | ||
* @return Chat ID | ||
*/ | ||
long id(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.blamer.hub.model; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Chats. | ||
* | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
public interface Chats { | ||
|
||
/** | ||
* Add new Chat. | ||
* | ||
* @param chat Chat | ||
* @return Mono void publisher | ||
*/ | ||
Mono<Void> add(Chat chat); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.blamer.hub.model; | ||
|
||
/** | ||
* Token. | ||
* | ||
* @author Aliaksei Bialiauski () | ||
* @since 0.0.0 | ||
*/ | ||
public interface Token { | ||
|
||
/** | ||
* Id. | ||
* | ||
* @return Token id | ||
*/ | ||
Long id(); | ||
|
||
/** | ||
* Value. | ||
* | ||
* @return Token value | ||
*/ | ||
String value(); | ||
|
||
/** | ||
* Alias. | ||
* | ||
* @return Token alias | ||
*/ | ||
String alias(); | ||
|
||
/** | ||
* Chat. | ||
* | ||
* @return Token chat | ||
*/ | ||
Chat chat(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.blamer.hub.model; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Tokens. | ||
* | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
public interface Tokens { | ||
|
||
/** | ||
* Register new token. | ||
* | ||
* @param token Token | ||
* @return Mono void publisher | ||
*/ | ||
Mono<Void> add(Token token); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2023 Blamer.io | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* Model. | ||
* | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
package io.blamer.hub.model; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.blamer.hub.pg; | ||
|
||
import io.blamer.hub.model.Chat; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* Chat in PostgreSQL. | ||
* | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
@RequiredArgsConstructor | ||
public class PgChat implements Chat { | ||
|
||
/** | ||
* Chat ID. | ||
*/ | ||
private final long id; | ||
|
||
@Override | ||
public long id() { | ||
return this.id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.blamer.hub.pg; | ||
|
||
import io.blamer.hub.model.Chat; | ||
import io.blamer.hub.model.Chats; | ||
import org.springframework.r2dbc.core.DatabaseClient; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Chats in PostgreSQL. | ||
* | ||
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) | ||
* @since 0.0.0 | ||
*/ | ||
@Component | ||
public class PgChats implements Chats { | ||
|
||
/** | ||
* Database Client. | ||
*/ | ||
private final DatabaseClient db; | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param dbc Database Client. | ||
*/ | ||
public PgChats(final DatabaseClient dbc) { | ||
this.db = dbc; | ||
} | ||
|
||
@Override | ||
public Mono<Void> add(final Chat chat) { | ||
return this.db.sql( | ||
"INSERT INTO chat (id) VALUES (:id)" | ||
).bind("id", chat.id()) | ||
.fetch() | ||
.first() | ||
.then(); | ||
} | ||
} |
Oops, something went wrong.