package net.devlab722.guestbook.server.controllers;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.devlab722.guestbook.api.Message;
import net.devlab722.guestbook.api.Metadata;
import net.devlab722.guestbook.server.backend.RedisBackend;

@Controller
@RequestMapping(path = "/api/v1/guestbook/messages")
public class GuestBookController {

    @Autowired
    RedisBackend redisBackend;

    private static final String HOSTNAME = System.getenv("HOSTNAME");

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> storeMessage(
            @RequestBody(required = false) Message message) {
        Message augmented = Message.builder()
                .content(message.getContent())
                .metadata(Metadata.builder()
                        .apiServerName(HOSTNAME)
                        .datetimeString(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now()))
                        .build())
                .build();
        redisBackend.storeMessage(augmented);
        return new ResponseEntity<>(null, HttpStatus.CREATED);
    }

    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public List<Message> getMessages() {
        return redisBackend.getAllMessages();
    }

}

@@ -1045,9 +1045,15 @@
 ity%3C
-?
+Message
 %3E st
@@ -1164,32 +1164,81 @@
 ssage.builder()%0A
+                .userName(message.getUserName())%0A
                 
@@ -1615,12 +1615,17 @@
 y%3C%3E(
-null
+augmented
 , Ht

return the posted augmented message when created