-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from Softawii/feature/template
Feature/template Closes #23
- Loading branch information
Showing
14 changed files
with
671 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
name: build | ||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
|
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
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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/softawii/capivara/core/TemplateManager.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,55 @@ | ||
package com.softawii.capivara.core; | ||
|
||
import com.softawii.capivara.entity.Template; | ||
import com.softawii.capivara.entity.Template.TemplateKey; | ||
import com.softawii.capivara.exceptions.TemplateAlreadyExistsException; | ||
import com.softawii.capivara.exceptions.TemplateDoesNotExistException; | ||
import com.softawii.capivara.services.TemplateService; | ||
import net.dv8tion.jda.api.interactions.commands.Command; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class TemplateManager { | ||
|
||
private final TemplateService templateService; | ||
|
||
public TemplateManager(TemplateService templateService) { | ||
this.templateService = templateService; | ||
} | ||
|
||
public Template create(Long guildId, String name, String json) throws TemplateAlreadyExistsException { | ||
return templateService.create(new Template(new TemplateKey(guildId, name), json)); | ||
} | ||
|
||
public Template update(Long guildId, String name, String json) throws TemplateDoesNotExistException { | ||
Template template = templateService.findById(new TemplateKey(guildId, name)); | ||
template.setJson(json); | ||
return templateService.update(template); | ||
} | ||
|
||
public void destroy(Long guildId, String name) throws TemplateDoesNotExistException { | ||
templateService.destroy(guildId, name); | ||
} | ||
|
||
public List<Template> findAllByGuildId(Long guildId) { | ||
return templateService.findAllByGuildId(guildId); | ||
} | ||
|
||
public Template findById(Long guildId, String name) throws TemplateDoesNotExistException { | ||
return templateService.findById(new TemplateKey(guildId, name)); | ||
} | ||
|
||
public boolean existsById(Long guildId, String name) { | ||
return templateService.existsById(new TemplateKey(guildId, name)); | ||
} | ||
|
||
public List<Command.Choice> autoCompleteTemplateName(Long guildId, String templateName) { | ||
return templateService.findAllByGuildId(guildId).stream() | ||
.map(template -> template.getTemplateKey().getName()) | ||
.filter(c -> c.startsWith(templateName)) | ||
.map(c -> new Command.Choice(c, c)) | ||
.toList(); | ||
} | ||
} |
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,88 @@ | ||
package com.softawii.capivara.entity; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Embeddable; | ||
import javax.persistence.EmbeddedId; | ||
import javax.persistence.Entity; | ||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
@Entity | ||
public class Template { | ||
|
||
@EmbeddedId | ||
private TemplateKey templateKey; | ||
|
||
@Embeddable | ||
public static class TemplateKey implements Serializable { | ||
@Column | ||
private Long guildId; | ||
|
||
@Column | ||
private String name; | ||
|
||
public TemplateKey() { | ||
} | ||
|
||
public TemplateKey(Long guildId, String name) { | ||
this.guildId = guildId; | ||
this.name = name; | ||
} | ||
|
||
public Long getGuildId() { | ||
return guildId; | ||
} | ||
|
||
public void setGuildId(Long guildId) { | ||
this.guildId = guildId; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
TemplateKey that = (TemplateKey) o; | ||
return guildId.equals(that.guildId) && name.equals(that.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(guildId, name); | ||
} | ||
} | ||
|
||
@Column | ||
private String json; | ||
|
||
public Template() { | ||
} | ||
|
||
public Template(TemplateKey templateKey, String json) { | ||
this.templateKey = templateKey; | ||
this.json = json; | ||
} | ||
|
||
public TemplateKey getTemplateKey() { | ||
return templateKey; | ||
} | ||
|
||
public void setTemplateKey(TemplateKey templateKey) { | ||
this.templateKey = templateKey; | ||
} | ||
|
||
public String getJson() { | ||
return json; | ||
} | ||
|
||
public void setJson(String json) { | ||
this.json = json; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/softawii/capivara/exceptions/CategoryIsEmptyException.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,22 @@ | ||
package com.softawii.capivara.exceptions; | ||
|
||
public class CategoryIsEmptyException extends Exception { | ||
public CategoryIsEmptyException() { | ||
} | ||
|
||
public CategoryIsEmptyException(String message) { | ||
super(message); | ||
} | ||
|
||
public CategoryIsEmptyException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public CategoryIsEmptyException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public CategoryIsEmptyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/softawii/capivara/exceptions/TemplateAlreadyExistsException.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,22 @@ | ||
package com.softawii.capivara.exceptions; | ||
|
||
public class TemplateAlreadyExistsException extends Exception { | ||
public TemplateAlreadyExistsException() { | ||
} | ||
|
||
public TemplateAlreadyExistsException(String message) { | ||
super(message); | ||
} | ||
|
||
public TemplateAlreadyExistsException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public TemplateAlreadyExistsException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public TemplateAlreadyExistsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/softawii/capivara/exceptions/TemplateDoesNotExistException.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,22 @@ | ||
package com.softawii.capivara.exceptions; | ||
|
||
public class TemplateDoesNotExistException extends Exception { | ||
public TemplateDoesNotExistException() { | ||
} | ||
|
||
public TemplateDoesNotExistException(String message) { | ||
super(message); | ||
} | ||
|
||
public TemplateDoesNotExistException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public TemplateDoesNotExistException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public TemplateDoesNotExistException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
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
Oops, something went wrong.