aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/DeleteMessage.java
blob: 500e18b3e758fddf015b488dc70e7335e2ee9b8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.juick.command;

import com.juick.User;
import com.juick.service.MessagesService;
import lombok.Getter;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

/**
 * @author ma1uta
 */
@Getter
@Component
public class DeleteMessage extends MultiArgsCommand {

    private static final Pattern PATTERN = Pattern.compile("^d\\s*\\#([0-9]+)$", Pattern.CASE_INSENSITIVE);

    private final MessagesService messagesService;

    @Autowired
    public DeleteMessage(MessagesService messagesService) {
        this.messagesService = messagesService;
    }

    @Override
    public Pattern pattern() {
        return PATTERN;
    }

    @Override
    public String help() {
        return "D #12345 - delete the message";
    }

    @Override
    protected String execute(User sender, MessageListener protocolListener, String... arguments) {
        int mid = NumberUtils.toInt(arguments[0], 0);
        if (getMessagesService().deleteMessage(sender.getUid(), mid)) {
            return String.format("Message %s deleted", mid);
        }
        return "Error";
    }
}