Add NoteService to handle note operations
NoteService should be the only class with access to NoteDao; nowhere else should NoteDao be accessed directly. Channel dependencies are static in PacketProcessor, for now. Ideally they would be injected in the constructor, but since the constructor is private and I don't want to open up that can of worms, I'll leave it like this. At the very least, now we have a way of injecting services into the handlers. This will make further restructuring way easier.
This commit is contained in:
93
src/main/java/service/NoteService.java
Normal file
93
src/main/java/service/NoteService.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package service;
|
||||
|
||||
import client.Character;
|
||||
import database.DaoException;
|
||||
import database.note.NoteDao;
|
||||
import model.Note;
|
||||
import net.packet.Packet;
|
||||
import net.server.Server;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tools.PacketCreator;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class NoteService {
|
||||
private static final Logger log = LoggerFactory.getLogger(NoteService.class);
|
||||
|
||||
private final NoteDao noteDao;
|
||||
|
||||
public NoteService(NoteDao noteDao) {
|
||||
this.noteDao = noteDao;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send normal note from one character to another
|
||||
*
|
||||
* @return Send success
|
||||
*/
|
||||
public boolean sendNormal(String message, String senderName, String receiverName) {
|
||||
Note normalNote = Note.createNormal(message, senderName, receiverName, Server.getInstance().getCurrentTime());
|
||||
return send(normalNote);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send note which will increase the receiver's fame by one.
|
||||
*
|
||||
* @return Send success
|
||||
*/
|
||||
public boolean sendWithFame(String message, String senderName, String receiverName) {
|
||||
Note noteWithFame = Note.createGift(message, senderName, receiverName, Server.getInstance().getCurrentTime());
|
||||
return send(noteWithFame);
|
||||
}
|
||||
|
||||
private boolean send(Note note) {
|
||||
try {
|
||||
NoteDao.save(note);
|
||||
return true;
|
||||
} catch (DaoException e) {
|
||||
log.error("Failed to send note {}", note, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show unread notes
|
||||
*
|
||||
* @param chr Note recipient
|
||||
*/
|
||||
public void show(Character chr) {
|
||||
if (chr == null) {
|
||||
throw new IllegalArgumentException("Unable to show notes - chr is null");
|
||||
}
|
||||
|
||||
final List<Note> notes;
|
||||
try {
|
||||
notes = noteDao.findAllByTo(chr.getName());
|
||||
} catch (DaoException e) {
|
||||
log.error("Failed to find notes sent to chr name {}", chr.getName(), e);
|
||||
return;
|
||||
}
|
||||
|
||||
Packet showNotesPacket = PacketCreator.showNotes(notes);
|
||||
chr.sendPacket(showNotesPacket);
|
||||
}
|
||||
|
||||
/**
|
||||
* Discard a read note
|
||||
*
|
||||
* @param id Id of note to discard
|
||||
* @return Discarded note. Empty optional if failed to discard.
|
||||
*/
|
||||
public Optional<Note> discard(int id) {
|
||||
try {
|
||||
return noteDao.delete(id);
|
||||
} catch (DaoException e) {
|
||||
log.error("Failed to discard note with id {}", id, e);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user