Skip to content

Commit

Permalink
Merge pull request #1 from sympoll/intial-service-build
Browse files Browse the repository at this point in the history
Intial service build
  • Loading branch information
Idan-sh authored Aug 14, 2024
2 parents c86fb38 + f5f2497 commit 66975a6
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 1 deletion.
57 changes: 57 additions & 0 deletions .github/workflows/RELEASE-docker-build-and-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: RELEASE - Build and push Docker official image to Github packages
on:
push:
branches:
- main
paths-ignore:
- "**/README.md"
- ".github/workflows/**"
- "**/.gitignore"
workflow_dispatch:

env:
IMAGE_NAME: ghcr.io/${{ github.repository }}/sympoll-group-service

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to GitHub Packages
run: echo "${{ secrets.PACKAGE_TOKEN }}" | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin

- name: Get current date
id: date
run: echo "::set-output name=DATE::$(date +'%d.%m.%Y')"

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: ~/.docker/builder
key: ${{ runner.os }}-build-${{ github.sha }} # Cache key based on OS and commit SHA
restore-keys: | # Check for existing cache based on previous builds with same OS and SHA
${{ runner.os }}-build-
- name: Build and push Docker image
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
file: Dockerfile
push: true
platforms: linux/amd64,linux/arm64
tags: |
${{ env.IMAGE_NAME }}:${{ steps.date.outputs.DATE }}
${{ env.IMAGE_NAME }}:latest
cache-from: type=gha,key=${{ runner.os }}-build-${{ github.sha }} # Use cached layers based on commit SHA
cache-to: type=gha,mode=max # Enable caching to GitHub Actions

49 changes: 49 additions & 0 deletions .github/workflows/TEST_CONTAINER-docker-build-and-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: TEST CONTAINER - Build and push Docker test image to Github packages
on:
workflow_dispatch:

env:
IMAGE_NAME: ghcr.io/${{ github.repository }}/sympoll-group-service-test

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to GitHub Packages
run: echo "${{ secrets.PACKAGE_TOKEN }}" | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin

- name: Get current date
id: date
run: echo "::set-output name=DATE::$(date +'%d.%m.%Y')"

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: ~/.docker/builder
key: ${{ runner.os }}-build-${{ github.sha }} # Cache key based on OS and commit SHA
restore-keys: | # Check for existing cache based on previous builds with same OS and SHA
${{ runner.os }}-build-
- name: Build and push Docker image
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
file: Dockerfile
push: true
platforms: linux/amd64,linux/arm64
tags: |
${{ env.IMAGE_NAME }}:${{ steps.date.outputs.DATE }}
${{ env.IMAGE_NAME }}:latest
cache-from: type=gha,key=${{ runner.os }}-build-${{ github.sha }} # Use cached layers based on commit SHA
cache-to: type=gha,mode=max # Enable caching to GitHub Actions
16 changes: 16 additions & 0 deletions docker/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Group Service Schema
CREATE TABLE groups
(
group_id VARCHAR(255) PRIMARY KEY,
group_name VARCHAR(255),
description TEXT,
creator_id UUID,
time_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE members
(
user_id uuid NOT NULL,
group_id VARCHAR(255) REFERENCES groups(group_id) ON DELETE CASCADE NOT NULL,
PRIMARY KEY (group_id, user_id)
);
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.2.4</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.MTAPizza.Sympoll.groupmanagementservice.controller;

import com.MTAPizza.Sympoll.groupmanagementservice.dto.request.GroupCreateRequest;
import com.MTAPizza.Sympoll.groupmanagementservice.dto.response.GroupResponse;
import com.MTAPizza.Sympoll.groupmanagementservice.service.GroupService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("api/group")
@RequiredArgsConstructor
public class ServiceController {
private final GroupService groupService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public GroupResponse createGroup(@RequestBody GroupCreateRequest groupCreateRequest) {
log.info("Received request to create a group");
log.debug("Group to create received: {}", groupCreateRequest);
return groupService.createGroup(groupCreateRequest);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.MTAPizza.Sympoll.groupmanagementservice.dto.request;

import java.util.UUID;

public record GroupCreateRequest(
String groupId, // can be null, if so then generate a group number
String groupName,
String description,
UUID creatorId
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.MTAPizza.Sympoll.groupmanagementservice.dto.response;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

public record GroupResponse (
String groupId,
String groupName,
String description,
UUID creatorId,
LocalDateTime timeCreated,
List<MemberResponse> membersList
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.MTAPizza.Sympoll.groupmanagementservice.dto.response;

import java.util.UUID;

public record MemberResponse(
UUID userId
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.MTAPizza.Sympoll.groupmanagementservice.model;

import com.MTAPizza.Sympoll.groupmanagementservice.dto.response.GroupResponse;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Entity
@Table(name = "groups")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String groupId;

@Column(name = "group_name")
private String groupName;

@Column(name = "description")
private String description;

@Column(name = "created_by_user")
private UUID creatorId;

@Column(name = "time_created")
private final LocalDateTime timeCreated = LocalDateTime.now(); // Initialize to the current time.

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "group_id")
private List<Member> membersList = new ArrayList<>(); // Initialize to an empty members list.

// TODO: Add Admins list, will be initialized with the creatorId as the only admin.

public GroupResponse toGroupResponse() {
return new GroupResponse(
groupId,
groupName,
description,
creatorId,
timeCreated,
membersList.stream().map(Member::toMemberResponse).toList() // Convert to member response
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.MTAPizza.Sympoll.groupmanagementservice.model;

import com.MTAPizza.Sympoll.groupmanagementservice.dto.response.MemberResponse;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;

@Entity
@Table(name = "members")
@NoArgsConstructor
@Data
public class Member {
@Column(name = "group_id")
private String groupId;

@Id
@Column(name = "user_id")
private UUID userId;

public MemberResponse toMemberResponse() {
return new MemberResponse(
userId
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.MTAPizza.Sympoll.groupmanagementservice.repository;

import com.MTAPizza.Sympoll.groupmanagementservice.model.Group;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface GroupRepository extends JpaRepository<Group, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.MTAPizza.Sympoll.groupmanagementservice.repository;

import com.MTAPizza.Sympoll.groupmanagementservice.model.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.MTAPizza.Sympoll.groupmanagementservice.service;

import com.MTAPizza.Sympoll.groupmanagementservice.dto.request.GroupCreateRequest;
import com.MTAPizza.Sympoll.groupmanagementservice.dto.response.GroupResponse;
import com.MTAPizza.Sympoll.groupmanagementservice.model.Group;
import com.MTAPizza.Sympoll.groupmanagementservice.repository.GroupRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class GroupService {
private final GroupRepository groupRepository;

public GroupResponse createGroup(GroupCreateRequest groupCreateRequest) {
// TODO: Validate request

// TODO: Add creator Id into a new list of admins
Group createdGroup = Group.builder()
.groupId(groupCreateRequest.groupId())
.groupName(groupCreateRequest.groupName())
.description(groupCreateRequest.description())
.creatorId(groupCreateRequest.creatorId())
.build();

groupRepository.save(createdGroup);
log.info("Group with ID - '{}' was created by - '{}'", createdGroup.getGroupId(), createdGroup.getCreatorId());

return createdGroup.toGroupResponse();
}
}
15 changes: 14 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
spring.application.name=group-management-service
server.port=8085
spring.application.name=group-service
spring.datasource.url=jdbc:postgresql://group-db:5435/postgres
spring.datasource.username=postgres
spring.datasource.password=1
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

# Use These Settings to Debug Spring Boot:
# ---------------------------------------
# logging.level.root=DEBUG
# logging.level.org.springframework.web=DEBUG
# logging.level.org.springframework=DEBUG
# logging.level.org.hibernate=ERROR

0 comments on commit 66975a6

Please sign in to comment.