SmsController.java 1.56 KB
package com.ecommerce.notification.controller;

import com.ecommerce.notification.model.dto.SmsRequest;
import com.ecommerce.notification.service.SmsService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/sms")
@RequiredArgsConstructor
public class SmsController {
    
    private final SmsService smsService;
    
    @PostMapping("/send")
    public ResponseEntity<Map<String, Object>> sendSms(@Valid @RequestBody SmsRequest request) {
        boolean success = smsService.sendSms(request);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "SMS sent successfully" : "Failed to send SMS");
        response.put("to", request.getTo());
        
        return ResponseEntity.ok(response);
    }
    
    @GetMapping("/test")
    public ResponseEntity<Map<String, String>> testSms() {
        SmsRequest request = new SmsRequest();
        request.setTo("+1234567890");
        request.setMessage("Test SMS from Notification Service");
        
        boolean success = smsService.sendSms(request);
        
        Map<String, String> response = new HashMap<>();
        response.put("status", success ? "SUCCESS" : "FAILED");
        response.put("message", success ? "Test SMS sent successfully" : "Failed to send test SMS");
        
        return ResponseEntity.ok(response);
    }
}