From 186b685ab55855a5fb85d735765643591d4c6a07 Mon Sep 17 00:00:00 2001 From: chanhengseang Date: Mon, 26 May 2025 11:38:40 -0700 Subject: [PATCH] return event --- .../exception/EntityNotFoundException.java | 8 +- .../db/migration/V11__add_event_tags.sql | 4 + pom.xml | 2 +- .../srr/converter/StringListConverter.java | 49 ++++++++++ sport/src/main/java/com/srr/domain/Event.java | 9 ++ sport/src/main/java/com/srr/dto/EventDto.java | 5 ++ .../main/java/com/srr/dto/JoinEventDto.java | 13 +++ .../java/com/srr/rest/EventController.java | 87 +++++++++++------- .../java/com/srr/service/EventService.java | 7 +- .../srr/service/impl/EventServiceImpl.java | 89 ++++++++++++------- 10 files changed, 203 insertions(+), 70 deletions(-) create mode 100644 eladmin-system/src/main/resources/db/migration/V11__add_event_tags.sql create mode 100644 sport/src/main/java/com/srr/converter/StringListConverter.java create mode 100644 sport/src/main/java/com/srr/dto/JoinEventDto.java diff --git a/eladmin-common/src/main/java/me/zhengjie/exception/EntityNotFoundException.java b/eladmin-common/src/main/java/me/zhengjie/exception/EntityNotFoundException.java index 4d9f4d96..2702ede8 100644 --- a/eladmin-common/src/main/java/me/zhengjie/exception/EntityNotFoundException.java +++ b/eladmin-common/src/main/java/me/zhengjie/exception/EntityNotFoundException.java @@ -23,12 +23,16 @@ import org.springframework.util.StringUtils; */ public class EntityNotFoundException extends RuntimeException { - public EntityNotFoundException(Class clazz, String field, String val) { + public EntityNotFoundException(Class clazz, String field, String val) { super(EntityNotFoundException.generateMessage(clazz.getSimpleName(), field, val)); } + public EntityNotFoundException(Class clazz, String field, Long val) { + super(EntityNotFoundException.generateMessage(clazz.getSimpleName(), field, "" + val)); + } + private static String generateMessage(String entity, String field, String val) { return StringUtils.capitalize(entity) - + " with " + field + " "+ val + " does not exist"; + + " with " + field + " " + val + " does not exist"; } } \ No newline at end of file diff --git a/eladmin-system/src/main/resources/db/migration/V11__add_event_tags.sql b/eladmin-system/src/main/resources/db/migration/V11__add_event_tags.sql new file mode 100644 index 00000000..3609626a --- /dev/null +++ b/eladmin-system/src/main/resources/db/migration/V11__add_event_tags.sql @@ -0,0 +1,4 @@ +-- Add tags column to event table +ALTER TABLE event + add column poster_image varchar(255), + ADD COLUMN tags VARCHAR(255) NULL COMMENT 'Tags stored as comma-delimited string'; diff --git a/pom.xml b/pom.xml index f636015f..2c0ce0c9 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ 1.2.9 UTF-8 UTF-8 - 1.8 + 17 2.0.54 1.2.19 2.11.1 diff --git a/sport/src/main/java/com/srr/converter/StringListConverter.java b/sport/src/main/java/com/srr/converter/StringListConverter.java new file mode 100644 index 00000000..2d39f80d --- /dev/null +++ b/sport/src/main/java/com/srr/converter/StringListConverter.java @@ -0,0 +1,49 @@ +/* +* Copyright 2019-2025 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.srr.converter; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Converter to store List as a comma-delimited string in database + * @author Chanheng + * @date 2025-05-26 + */ +@Converter +public class StringListConverter implements AttributeConverter, String> { + + private static final String DELIMITER = ","; + + @Override + public String convertToDatabaseColumn(List stringList) { + if (stringList == null || stringList.isEmpty()) { + return null; + } + return String.join(DELIMITER, stringList); + } + + @Override + public List convertToEntityAttribute(String string) { + if (string == null || string.isEmpty()) { + return new ArrayList<>(); + } + return new ArrayList<>(Arrays.asList(string.split(DELIMITER))); + } +} diff --git a/sport/src/main/java/com/srr/domain/Event.java b/sport/src/main/java/com/srr/domain/Event.java index b4ee0a61..facfa69d 100644 --- a/sport/src/main/java/com/srr/domain/Event.java +++ b/sport/src/main/java/com/srr/domain/Event.java @@ -15,6 +15,7 @@ */ package com.srr.domain; +import com.srr.converter.StringListConverter; import com.srr.enumeration.EventStatus; import com.srr.enumeration.Format; import lombok.Data; @@ -24,6 +25,7 @@ import cn.hutool.core.bean.copier.CopyOptions; import javax.persistence.*; import javax.persistence.Entity; import javax.persistence.Table; +import javax.persistence.Convert; import org.hibernate.annotations.*; import java.sql.Timestamp; import javax.validation.constraints.NotBlank; @@ -138,6 +140,13 @@ public class Event implements Serializable { @Column(name = "`allow_wait_list`") private boolean allowWaitList; + @Column(name = "`poster_image`") + private String posterImage; + + @Column(name = "`tags`") + @Convert(converter = StringListConverter.class) + private List tags = new ArrayList<>(); + @ManyToMany @JoinTable(name = "event_co_host_player", joinColumns = {@JoinColumn(name = "event_id",referencedColumnName = "id")}, diff --git a/sport/src/main/java/com/srr/dto/EventDto.java b/sport/src/main/java/com/srr/dto/EventDto.java index 6b0361e7..b0146795 100644 --- a/sport/src/main/java/com/srr/dto/EventDto.java +++ b/sport/src/main/java/com/srr/dto/EventDto.java @@ -92,7 +92,12 @@ public class EventDto implements Serializable { @ApiModelProperty(value = "Number of groups") private Integer groupCount; + + private String posterImage; @ApiModelProperty(value = "Co-host players") private List coHostPlayers; + + @ApiModelProperty(value = "Tags") + private List tags; } \ No newline at end of file diff --git a/sport/src/main/java/com/srr/dto/JoinEventDto.java b/sport/src/main/java/com/srr/dto/JoinEventDto.java new file mode 100644 index 00000000..6090db72 --- /dev/null +++ b/sport/src/main/java/com/srr/dto/JoinEventDto.java @@ -0,0 +1,13 @@ +package com.srr.dto; + +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@Data +public class JoinEventDto { + private Long playerId; + @NotNull + private Long eventId; + private Long teamId; +} diff --git a/sport/src/main/java/com/srr/rest/EventController.java b/sport/src/main/java/com/srr/rest/EventController.java index 296520cd..b2e36ebf 100644 --- a/sport/src/main/java/com/srr/rest/EventController.java +++ b/sport/src/main/java/com/srr/rest/EventController.java @@ -1,42 +1,47 @@ /* -* Copyright 2019-2025 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2025 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.srr.rest; -import me.zhengjie.annotation.Log; import com.srr.domain.Event; -import com.srr.service.EventService; +import com.srr.dto.EventDto; import com.srr.dto.EventQueryCriteria; -import org.springframework.data.domain.Pageable; +import com.srr.dto.JoinEventDto; +import com.srr.enumeration.EventStatus; +import com.srr.service.EventService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; import lombok.RequiredArgsConstructor; +import me.zhengjie.annotation.Log; +import me.zhengjie.utils.PageResult; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.*; -import java.io.IOException; + import javax.servlet.http.HttpServletResponse; -import me.zhengjie.utils.PageResult; -import com.srr.dto.EventDto; +import java.io.IOException; /** -* @website https://eladmin.vip -* @author Chanheng -* @date 2025-05-18 -**/ + * @author Chanheng + * @website https://eladmin.vip + * @date 2025-05-18 + **/ @RestController @RequiredArgsConstructor @Api(tags = "event") @@ -55,26 +60,42 @@ public class EventController { @GetMapping @ApiOperation("Query event") @PreAuthorize("@el.check('event:list')") - public ResponseEntity> queryEvent(EventQueryCriteria criteria, Pageable pageable){ - return new ResponseEntity<>(eventService.queryAll(criteria,pageable),HttpStatus.OK); + public ResponseEntity> queryEvent(EventQueryCriteria criteria, Pageable pageable) { + return new ResponseEntity<>(eventService.queryAll(criteria, pageable), HttpStatus.OK); } @PostMapping @Log("Add event") @ApiOperation("Add event") @PreAuthorize("@el.check('event:add')") - public ResponseEntity createEvent(@Validated @RequestBody Event resources){ - eventService.create(resources); - return new ResponseEntity<>(HttpStatus.CREATED); + public ResponseEntity createEvent(@Validated @RequestBody Event resources) { + final var result = eventService.create(resources); + return new ResponseEntity<>(result, HttpStatus.CREATED); } @PutMapping @Log("Modify event") @ApiOperation("Modify event") @PreAuthorize("@el.check('event:edit')") - public ResponseEntity updateEvent(@Validated @RequestBody Event resources){ - eventService.update(resources); - return new ResponseEntity<>(HttpStatus.NO_CONTENT); + public ResponseEntity updateEvent(@Validated @RequestBody Event resources) { + final var result = eventService.update(resources); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + @PatchMapping("/{id}/status/{status}") + @Log("Update event status") + @ApiOperation("Update event status") + @PreAuthorize("@el.check('event:edit')") + public ResponseEntity updateEventStatus( + @PathVariable Long id, + @PathVariable EventStatus status) { + final var result = eventService.updateStatus(id, status); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + public ResponseEntity joinEvent(@PathVariable Long id, @RequestBody JoinEventDto joinEventDto) { + final EventDto result = eventService.joinEvent(joinEventDto); + return new ResponseEntity<>(result, HttpStatus.OK); } @DeleteMapping diff --git a/sport/src/main/java/com/srr/service/EventService.java b/sport/src/main/java/com/srr/service/EventService.java index dec18861..ff0e5e08 100644 --- a/sport/src/main/java/com/srr/service/EventService.java +++ b/sport/src/main/java/com/srr/service/EventService.java @@ -18,6 +18,7 @@ package com.srr.service; import com.srr.domain.Event; import com.srr.dto.EventDto; import com.srr.dto.EventQueryCriteria; +import com.srr.enumeration.EventStatus; import org.springframework.data.domain.Pageable; import java.util.List; @@ -59,13 +60,15 @@ public interface EventService { * Create * @param resources / */ - void create(Event resources); + EventDto create(Event resources); /** * Edit * @param resources / */ - void update(Event resources); + EventDto update(Event resources); + + EventDto updateStatus(Long id, EventStatus status); /** * Multi-select delete diff --git a/sport/src/main/java/com/srr/service/impl/EventServiceImpl.java b/sport/src/main/java/com/srr/service/impl/EventServiceImpl.java index 36285778..1e94248d 100644 --- a/sport/src/main/java/com/srr/service/impl/EventServiceImpl.java +++ b/sport/src/main/java/com/srr/service/impl/EventServiceImpl.java @@ -1,21 +1,23 @@ /* -* Copyright 2019-2025 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2025 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.srr.service.impl; import com.srr.domain.Event; +import com.srr.enumeration.EventStatus; +import me.zhengjie.exception.EntityNotFoundException; import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.FileUtil; import lombok.RequiredArgsConstructor; @@ -30,20 +32,24 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import me.zhengjie.utils.PageUtil; import me.zhengjie.utils.QueryHelp; + +import java.sql.Timestamp; +import java.time.Instant; import java.util.List; import java.util.Map; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.LinkedHashMap; + import me.zhengjie.utils.PageResult; /** -* @website https://eladmin.vip -* @description 服务实现 -* @author Chanheng -* @date 2025-05-18 -**/ + * @author Chanheng + * @website https://eladmin.vip + * @description 服务实现 + * @date 2025-05-18 + **/ @Service @RequiredArgsConstructor public class EventServiceImpl implements EventService { @@ -52,37 +58,56 @@ public class EventServiceImpl implements EventService { private final EventMapper eventMapper; @Override - public PageResult queryAll(EventQueryCriteria criteria, Pageable pageable){ - Page page = eventRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); + public PageResult queryAll(EventQueryCriteria criteria, Pageable pageable) { + Page page = eventRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable); return PageUtil.toPage(page.map(eventMapper::toDto)); } @Override - public List queryAll(EventQueryCriteria criteria){ - return eventMapper.toDto(eventRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); + public List queryAll(EventQueryCriteria criteria) { + return eventMapper.toDto(eventRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder))); } @Override @Transactional public EventDto findById(Long id) { Event event = eventRepository.findById(id).orElseGet(Event::new); - ValidationUtil.isNull(event.getId(),"Event","id",id); + ValidationUtil.isNull(event.getId(), "Event", "id", id); return eventMapper.toDto(event); } @Override @Transactional(rollbackFor = Exception.class) - public void create(Event resources) { - eventRepository.save(resources); + public EventDto create(Event resources) { + resources.setStatus(EventStatus.DRAFT); + final var result = eventRepository.save(resources); + return eventMapper.toDto(result); } @Override @Transactional(rollbackFor = Exception.class) - public void update(Event resources) { + public EventDto update(Event resources) { Event event = eventRepository.findById(resources.getId()).orElseGet(Event::new); - ValidationUtil.isNull( event.getId(),"Event","id",resources.getId()); + ValidationUtil.isNull(event.getId(), "Event", "id", resources.getId()); event.copy(resources); - eventRepository.save(event); + final var result = eventRepository.save(event); + return eventMapper.toDto(result); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public EventDto updateStatus(Long id, EventStatus status) { + Event event = eventRepository.findById(id) + .orElseThrow(() -> new EntityNotFoundException(Event.class, "id", id)); + + // Only update the status field + event.setStatus(status); + if (status == EventStatus.CHECK_IN) { + event.setCheckInAt(Timestamp.from(Instant.now())); + } + + final var result = eventRepository.save(event); + return eventMapper.toDto(result); } @Override @@ -96,7 +121,7 @@ public class EventServiceImpl implements EventService { public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (EventDto event : all) { - Map map = new LinkedHashMap<>(); + Map map = new LinkedHashMap<>(); map.put("名称", event.getName()); map.put("描述", event.getDescription()); map.put("SINGLE, DOUBLE", event.getFormat()); @@ -108,8 +133,8 @@ public class EventServiceImpl implements EventService { map.put("排序", event.getSort()); map.put("是否启用", event.getEnabled()); map.put("时间", event.getEventTime()); - map.put(" clubId", event.getClubId()); - map.put(" createBy", event.getCreateBy()); + map.put(" clubId", event.getClubId()); + map.put(" createBy", event.getCreateBy()); list.add(map); } FileUtil.downloadExcel(list, response);