MirrorRegistrationService.java

/*
 * Copyright © 2023-2025 The CTAN Team and individual authors
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */
package org.ctan.site.services.mirrors;

import java.time.LocalDateTime;
import java.util.Map;

import org.ctan.site.domain.mirrors.MirrorRegistration;
import org.ctan.site.services.mail.MailException;
import org.ctan.site.services.mail.MailService;
import org.ctan.site.services.mail.MailService.Mail;
import org.ctan.site.stores.MirrorRegistrationStore;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.NonNull;

/**
 * The class <code>MirrorRegistrationService</code> contains the service to
 * register a mirror.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
public class MirrorRegistrationService {

    /**
     * The field <code>store</code> contains the mirror registration store.
     */
    private MirrorRegistrationStore store;

    /**
     * The field <code>mailService</code> contains the mail service.
     */
    private MailService mailService;

    /**
     * This is the constructor for the class
     * <code>MirrorRegistrationService</code>.
     *
     * @param store the underlying store
     * @param mailService the mail service
     */
    @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW")
    public MirrorRegistrationService(@NonNull MirrorRegistrationStore store,
        @NonNull MailService mailService) {

        this.store = store;
        this.mailService = mailService;
    }

    /**
     * The method <code>registration</code> provides means to store a
     * registration.
     *
     * @param registration the registration
     * @return the mirror registration
     * @throws MailException in case of an error
     */
    public MirrorRegistration registration(
        @NonNull MirrorRegistration registration)
        throws MailException {

        registration.setDateCreated(LocalDateTime.now());
        registration = store.save(registration);
        mailService.send(Mail.builder()
            .type("mirror-registration")
            .model(Map.of("registration", registration))
            .html(false)
            .build());
        return registration;
    }
}