Campaign Init setting_ apply to RestfulAPI

the Other Object·2023년 5월 3일
0
// aggregate/Campaign.java

public class Campaign {                                        
    //
    private String id;
    private String name;
    //
    private Period period;
    private List<Product> products;

    public Campaign(CreateCampaign command) {
        //
        this.id = command.getId();
        this.name = command.getName();
        this.period = command.getPeriod();
        //
        this.products = new ArrayList<>();
    }

    public void change(UpdateCampaign command) {
        //
        this.name = command.getName();
        this.period = command.getPeriod();
    }

    public void attachProduct(AttachProduct command) {
        //
        this.products.add(command.getProduct());
    }

    public void detachProduct(DetachProduct command) {
        //
        Optional<Product> product = this.findProduct(command.getProductNo());
        if (product.isPresent()) {
            this.products.remove(product);
        }
    }

    private Optional<Product> findProduct(String productNo) {
        //
        return this.products.stream()
                .filter((product) -> { return productNo.equals(product.getNo()); })
                .findFirst();
    }
}


//aggregate/Product.java

public class Product {
    //
    private String no;
    private String name;
    private int listPrice;
    private int salesPrice;
}

//Campaign.tsx



import React, { useEffect, useState } from "react";
import axios from "axios";
import { useMutation, useQuery } from "react-query";

// const Campaign = () => {
//   const [campaignProducts, setCampaignProducts] = useState();

//   const { isLoading, error, data, isFetching, refetch } = useQuery({
//     queryKey: ["campaignProducts"],
//     queryFn: () => queryCampaignProducts(),
//   });
//   const queryCampaignProducts = async () => {
//     const response = await axios.get(
//       `/api/campaign/dsstore/dssdlabs/-/campaign`,
//       {
//         headers: {
//           "Content-Type": "application/json",
//           service: "campaign",
//           query: "QueryCampaign",
//         },
//       }
//     );
//     return setCampaignProducts(response.data);
//   };
// };

// export default Campaign;

interface CampaignType {
  campaignId: string;
  product: string;
  productNo: string;
  id: string;
  name: string;
  period: string;
}

//AttachProduct.java
public class AttachProduct extends Command {
    //
    private String campaignId;
    private Product product;
}

//Campaign.tsx
const Campaign = () => {
  const AttachProduct = () => {
    fetch("/api/campaign/dsstore/dssdlabs/-/campaign", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        service: "campaign",
        command: "AttachProduct",
      },
      body: JSON.stringify({
        campaignId: "campaignId",
        product: {
          no: "",
          name: "",
          listPrice: 1000,
          salesPrice: 1000,
        },
      }),
    }).then((response) => {
      if (response.status === 200) {
        console.log("성공");
      } else {
        console.log("실패");
      }
    });
  };

CampaignEndpoint.java

 @PostMapping(value = "/{universeName}/**", headers = {"command=AttachProduct"})
 public void attachProduct(@RequestBody AttachProduct command, @PathVariable String universeName, HttpServletRequest httpServletRequest) {
     //
     final String path = httpServletRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
     final String bestMatchingPattern = httpServletRequest.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString();
     String spaceName = new AntPathMatcher().extractPathWithinPattern(bestMatchingPattern, path).replace(PREFIX, "");

    this.campaignService.attachProduct(command);
 }

//CreateCampaign.java
public class CreateCampaign extends Command {
    //
    private transient String id;
    private String name;
    private Period period;
}

//Campaign.tsx
  const CreateCampaign = async () => {
    const response = await fetch("api/campaign/dsstore/dssdlabs/-/campaign", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        service: "campaign",
        command: "CreateCampaign",
      },
      body: JSON.stringify({
        name: "name",
        period: {
          from: 111,
          to: 111,
        },
      }),
    });
    if (response.status === 200) {
      console.log("response.body: ", response.body);
    } else {
      console.log(response);
    }
  };

CampaignEndpoint.java

 @PostMapping(value = "/{universeName}/**", headers = {"command=CreateCampaign"})
 public String createCampaign(@RequestBody CreateCampaign command, @PathVariable String universeName, HttpServletRequest httpServletRequest) {
     //
     final String path = httpServletRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
     final String bestMatchingPattern = httpServletRequest.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString();
     String spaceName = new AntPathMatcher().extractPathWithinPattern(bestMatchingPattern, path).replace(PREFIX, "");

     return this.campaignService.createCampaign(command);
 }

//DetachProduct.java
public class DetachProduct extends Command {
    //
    private String campaignId;
    private String productNo;
}

//Campaign.tsx
  const DetachProduct = () => {
    fetch("api/campaign/dsstore/dssdlabs/-/campaign", {
      method: "DELETE",
      headers: {
        "Content-Type": "application/json",
        service: "campaign",
        command: "DetachProduct",
      },
      body: JSON.stringify({
        campaignId: "campaignId",
        productNo: "productNo",
      }),
    });
  };

CampaignEndpoint.java

 @DeleteMapping(value = "/{universeName}/**", headers = {"command=DetachProduct"})
 public void detachProduct(@RequestBody DetachProduct command, @PathVariable String universeName, HttpServletRequest httpServletRequest) {
     //
     final String path = httpServletRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
     final String bestMatchingPattern = httpServletRequest.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString();
     String spaceName = new AntPathMatcher().extractPathWithinPattern(bestMatchingPattern, path).replace(PREFIX, "");

     this.campaignService.detachProduct(command);
 }

//UpdateCampaign.java
public class UpdateCampaign extends Command {
    //
    private String id;
    private String name;
    private Period period;
}

//Campaign.tsx
  const UpdateCampaign = () => {
    fetch("api/campaign/dsstore/dssdlabs/-/campaign", {
      method: "PUT",
      headers: {
        "Content-Type": "application/json",
        service: "campaign",
        command: "UpdateCampaign",
      },
      body: JSON.stringify({
        campaignId: "campaignId",
        productNo: "productNo",
      }),
    });
  };

CampaignEndpoint.java

??????????????????


//QueryCampaign.java
public class QueryCampaign extends Query {
    //
    private transient String campaignId;
}


//Campaign.tsx
  const QueryCampaign = () => {
    fetch("api/campaign/dsstore/dssdlabs/-/campaign?campaignId=${'1'}", {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        service: "campaign",
        query: "QueryCampaign",
      },
      // body: JSON.stringify({
      //   campaignId: campaignId,
      //   productNo: productNo,
      // }),
    });
  };
};

export default Campaign;

CampaignEndpoint.java

 @GetMapping(value = "/{universeName}/**", headers = {"query=QueryCampaign"})
 public Campaign queryCampaign(@RequestParam String campaignId, @PathVariable String universeName, HttpServletRequest httpServletRequest) {
     //
     final String path = httpServletRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
     final String bestMatchingPattern = httpServletRequest.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString();
     String spaceName = new AntPathMatcher().extractPathWithinPattern(bestMatchingPattern, path).replace(PREFIX, "");

     QueryCampaign query = new QueryCampaign(campaignId);
     return this.campaignService.query(query);
 }

0개의 댓글