04tudor / loyalty-campaign-service

Java project Loyalty Campaign Service using Hexagonal Architecture
0 stars 0 forks source link

create rest api for register campaign #26

Closed DemiusAcademius closed 6 months ago

DemiusAcademius commented 7 months ago

in package md.maib.retail.rest

create rest resource as @RestController and method for register campaign

04tudor commented 7 months ago

package md.maib.retail.infrastructure.rest;

import lombok.RequiredArgsConstructor; import md.maib.retail.application.CampaignAllInfo; import md.maib.retail.application.CampaignSomeInfo; import md.maib.retail.application.campaigns_list_by_date.CampaignsListByDateUseCase; import md.maib.retail.application.delete_campaign.DeleteCampaign; import md.maib.retail.application.delete_campaign.DeleteCampaignUseCase; import md.maib.retail.application.find_campaign_by_id.FindCampaignByIdUseCase; import md.maib.retail.application.find_campaign_by_metainfo.FindCampaignByMetaInfoUseCase; import md.maib.retail.application.register_newcampaign.RegisterCampaign; import md.maib.retail.application.register_newcampaign.RegistrationCampaignUseCase; import md.maib.retail.model.campaign.CampaignId; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate; import java.util.List; import java.util.Optional;

import static org.springframework.http.HttpStatus.NO_CONTENT; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.http.ResponseEntity.created; import static org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromCurrentRequest;

@RestController @RequestMapping("/campaigns") @RequiredArgsConstructor final class CampaignController {

private final FindCampaignByIdUseCase findCampaignByIdUseCase;
private final FindCampaignByMetaInfoUseCase findCampaignByMetaInfoUseCase;
private final RegistrationCampaignUseCase registrationCampaignUseCase;
private final DeleteCampaignUseCase deleteCampaignUseCase;
private final CampaignsListByDateUseCase campaignsListByDateUseCase;

@PostMapping(consumes = APPLICATION_JSON_VALUE)
ResponseEntity<?> create(@RequestBody RegisterCampaign request) {
    var campaignId = registrationCampaignUseCase.registerCampaign(request);
    return created(fromCurrentRequest().path("/{campaignId}").build(campaignId)).build();
}

@GetMapping(path = "/metainfo", produces = APPLICATION_JSON_VALUE)
List<CampaignAllInfo> findCampaignByMetaInfo(String key, String value) {
    return findCampaignByMetaInfoUseCase.findByMetaInfo(key,value);
}
@GetMapping(path = "/date", produces = APPLICATION_JSON_VALUE)
List<CampaignSomeInfo> ListByDate(LocalDate date) {
    return campaignsListByDateUseCase.activeCampaignsByDate(date);
}

@GetMapping(path = "/{campaignId}", produces = APPLICATION_JSON_VALUE)
Optional<CampaignAllInfo> findById(@PathVariable("campaignId") CampaignId campaignId) {
    return findCampaignByIdUseCase.findById(campaignId);
}

@DeleteMapping(path = "/{campaignId}")
@ResponseStatus(NO_CONTENT)
void delete(@PathVariable("campaignId") DeleteCampaign campaignId) {
    deleteCampaignUseCase.deleteCampaign(campaignId);
}

}