Skip to content

Commit

Permalink
feat: create-post implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicente1215 committed Apr 21, 2023
1 parent e53a72f commit 31d7f6e
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public ResponseEntity<Post> createPost(Authentication auth, @RequestBody Data co
author = userService.findByID(author.getId()).orElseThrow();
List<PostItem> items = content.getItems();
for (PostItem item: items) {
item.setImage(pathToImage(item.getImagePath()));
postItemService.save(item);
}
List<Topic> topicList = topicService.getTopics(content.getTopicStrings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ResponseEntity<Topic> getTopic(@PathVariable long topicId) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@GetMapping("/}")
@GetMapping("")
public ResponseEntity<List<Topic>> getTopics() {
List<Topic> all = topicService.findAll();
return new ResponseEntity<>(all, HttpStatus.OK);
Expand Down
28 changes: 20 additions & 8 deletions back/src/main/java/net/daw/alist/models/PostItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Base64;

import javax.persistence.*;
import javax.sql.rowset.serial.SerialBlob;

import ch.qos.logback.classic.Logger;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
Expand All @@ -21,7 +24,6 @@ public class PostItem {
private Long id;

private String description;
private String imagePath;

@Lob
@JsonIgnore
Expand All @@ -32,12 +34,21 @@ public PostItem() { }

public PostItem(
String description,
String imagePath
String imagePath,
boolean bool
) throws IOException, SQLException {
this.description = description;
setImage(imagePath);
}

public PostItem(
String description,
String base64Image
) throws IOException, SQLException {
this.description = description;
setImage(base64ToBlob(base64Image));
}

public PostItem(
String description,
Blob image
Expand All @@ -49,9 +60,6 @@ public PostItem(
public void setDescription(String description) {
this.description = description;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}

public void setImage(String imagePath) throws IOException, SQLException {
if (imagePath == null) {
Expand All @@ -60,16 +68,20 @@ public void setImage(String imagePath) throws IOException, SQLException {
this.image = pathToImage(imagePath);
}

public Blob base64ToBlob(String base64Image) throws SQLException {
String base64Data = base64Image.substring(base64Image.indexOf(",") + 1);
byte[] imageBytes = Base64.getDecoder().decode(base64Data);
Blob blob = new SerialBlob(imageBytes);
return blob;
}

public void setImage(Blob image) {
this.image = image;
}

public String getDescription() {
return description;
}
public String getImagePath() {
return imagePath;
}
public Blob getImage() {
return image;
}
Expand Down
50 changes: 25 additions & 25 deletions back/src/main/java/net/daw/alist/services/DatabaseInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,31 +108,31 @@ public void init() throws IOException, SQLException {
topicRepository.save(programming);
topicRepository.save(f1);

PostItem attackOnTitan = new PostItem("Attack on Titan", "static/images/example/attackOnTitan.jpg");
PostItem fullMetalAlchemist = new PostItem("Fullmetal Alchemist", "static/images/example/fullmetalAlchemist.jpg");
PostItem inazumaEleven = new PostItem("Inazuma Eleven", "static/images/example/inazumaEleven.jpg");
PostItem myLittlePony = new PostItem("My Little Pony", "static/images/example/myLittlePony.jpg");
PostItem onePiece = new PostItem("One Piece", "static/images/example/onePiece.jpg");
PostItem giannisAntetokounmpo = new PostItem("Giannis Antetokounmpo", "static/images/example/gianis.jpg");
PostItem lebronJames = new PostItem("Lebron James", "static/images/example/lebron.jpg");
PostItem stephenCurry = new PostItem("Stephen Curry", "static/images/example/steph.jpg");
PostItem kevinDurant = new PostItem("Kevin Durant", "static/images/example/kevinDurant.jpg");
PostItem antonyDavis = new PostItem("Antony Davis", "static/images/example/anthonyDavis.jpg");
PostItem getafe = new PostItem("Getafe", "static/images/example/getafe.jpg");
PostItem rayoVayecano = new PostItem("Rayo Vayecano", "static/images/example/rayoVayecano.jpg");
PostItem betis = new PostItem("Betis", "static/images/example/betis.jpg");
PostItem realMadrid = new PostItem("Real Madrid", "static/images/example/realMadrid.jpg");
PostItem barcelona = new PostItem("Barcelona", "static/images/example/barsa.jpg");
PostItem cr71 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg");
PostItem cr72 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg");
PostItem cr73 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg");
PostItem cr74 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg");
PostItem cr75 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg");
PostItem shanks1 = new PostItem("Hakiman", "static/images/example/hakiman.webp");
PostItem shanks2 = new PostItem("Godking","static/images/example/hakiman.webp");
PostItem shanks3 = new PostItem("Shanks","static/images/example/hakiman.webp");
PostItem shanks4 = new PostItem("Red hair","static/images/example/hakiman.webp");
PostItem shanks5 = new PostItem("One shot man","static/images/example/hakiman.webp");
PostItem attackOnTitan = new PostItem("Attack on Titan", "static/images/example/attackOnTitan.jpg", true);
PostItem fullMetalAlchemist = new PostItem("Fullmetal Alchemist", "static/images/example/fullmetalAlchemist.jpg", true);
PostItem inazumaEleven = new PostItem("Inazuma Eleven", "static/images/example/inazumaEleven.jpg", true);
PostItem myLittlePony = new PostItem("My Little Pony", "static/images/example/myLittlePony.jpg", true);
PostItem onePiece = new PostItem("One Piece", "static/images/example/onePiece.jpg", true);
PostItem giannisAntetokounmpo = new PostItem("Giannis Antetokounmpo", "static/images/example/gianis.jpg", true);
PostItem lebronJames = new PostItem("Lebron James", "static/images/example/lebron.jpg", true);
PostItem stephenCurry = new PostItem("Stephen Curry", "static/images/example/steph.jpg", true);
PostItem kevinDurant = new PostItem("Kevin Durant", "static/images/example/kevinDurant.jpg", true);
PostItem antonyDavis = new PostItem("Antony Davis", "static/images/example/anthonyDavis.jpg", true);
PostItem getafe = new PostItem("Getafe", "static/images/example/getafe.jpg", true);
PostItem rayoVayecano = new PostItem("Rayo Vayecano", "static/images/example/rayoVayecano.jpg", true);
PostItem betis = new PostItem("Betis", "static/images/example/betis.jpg", true);
PostItem realMadrid = new PostItem("Real Madrid", "static/images/example/realMadrid.jpg", true);
PostItem barcelona = new PostItem("Barcelona", "static/images/example/barsa.jpg", true);
PostItem cr71 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg", true);
PostItem cr72 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg", true);
PostItem cr73 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg", true);
PostItem cr74 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg", true);
PostItem cr75 = new PostItem("Cristiano Ronaldo", "static/images/example/cr7.jpg", true);
PostItem shanks1 = new PostItem("Hakiman", "static/images/example/hakiman.webp", true);
PostItem shanks2 = new PostItem("Godking","static/images/example/hakiman.webp", true);
PostItem shanks3 = new PostItem("Shanks","static/images/example/hakiman.webp", true);
PostItem shanks4 = new PostItem("Red hair","static/images/example/hakiman.webp", true);
PostItem shanks5 = new PostItem("One shot man","static/images/example/hakiman.webp", true);

postItemRepository.save(attackOnTitan);
postItemRepository.save(onePiece);
Expand Down
4 changes: 0 additions & 4 deletions front/src/app/post/interfaces/createPost.interface.ts

This file was deleted.

9 changes: 9 additions & 0 deletions front/src/app/post/interfaces/post.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PostItem } from './postItem.interface';

export interface Post {
title: string;
topicStrings: string[];
items: PostItem[];
}


4 changes: 4 additions & 0 deletions front/src/app/post/interfaces/postItem.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface PostItem {
description: string;
imagePath: string;
}
4 changes: 2 additions & 2 deletions front/src/app/post/interfaces/topic.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Topic {
id: number;
name: String;
description: String;
name: string;
description: string;
}
13 changes: 13 additions & 0 deletions front/src/app/post/pages/create-post/create-post.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

ul {
z-index: 1000; /* Ajusta este valor según sea necesario */
position: relative;
}

li {
background-color: black; /* Ajusta este valor según sea necesario */
padding: 5px;
border: 1px solid #ccc;
}



.btn {
padding: 10px 20px; /* Ajusta estos valores para cambiar el tamaño del botón */
font-size: 1.2em; /* Ajusta este valor para cambiar el tamaño del texto del botón */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
<h2 class="form-title" [ngStyle]="{'color': '#2d474f', 'text-align': 'center'}">Create a new Post</h2>
<form id='createPostForm' [formGroup]='myForm' (ngSubmit)='onSubmit()'>
<input class='title form-control mt-3 mb-2' id='topTitle' formControlName='title' placeholder='Write the title...'>
<select [(ngModel)]="selectedTopics" name="topics" multiple>
<option *ngFor="let topic of topics" [ngValue]="topic">{{ topic.name }}</option>
<select formControlName="selectedTopics" multiple (change)="onTopicsChange($event)">
<option *ngFor="let option of topics" [value]="option.name">{{ option.name }}</option>
</select>
<ul>
<li *ngFor="let selectedTopic of selectedTopics">{{ selectedTopic }}</li>
</ul>

<div formArrayName='descriptionsAndImages'>
<ng-container *ngFor='let item of descriptionsAndImagesControls; let i = index;'>
<div [formGroupName]='i'>
Expand All @@ -19,3 +23,4 @@ <h4 class="description-title" [ngStyle]="{'color': '#2d474f'}">Top #{{i + 1}}</h

</form>
</div>

42 changes: 36 additions & 6 deletions front/src/app/post/pages/create-post/create-post.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';
import { FormBuilder, FormGroup, FormArray, FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { TopicsService } from '../../services/topic.service';
import { Topic } from '../../interfaces/topic.interface';

import { PostService } from '../../services/post.service';
import { Post } from '../../interfaces/post.interface';


@Component({
Expand All @@ -15,16 +16,17 @@ export class CreatePostComponent implements OnInit {
myForm!: FormGroup;

topics: Topic[] = [];
selectedTopics: Topic[] = [];

constructor(private topicsService: TopicsService, private router: Router, private formBuilder: FormBuilder) { }
constructor(private postService: PostService, private topicsService: TopicsService, private router: Router, private formBuilder: FormBuilder) { }


ngOnInit() {
this.topicsService.getTopics().subscribe((topics: Topic[]) => {
this.topics = topics;
});
this.myForm = this.formBuilder.group({
'title': [null],
'selectedTopics': this.formBuilder.control([]), // <-- Agrega el control de formulario para el select
'descriptionsAndImages': this.formBuilder.array([
this.createDescriptionAndImageGroup(),
this.createDescriptionAndImageGroup(),
Expand All @@ -35,6 +37,16 @@ export class CreatePostComponent implements OnInit {
});
}

get selectedTopics(): string[] {
return this.myForm.controls['selectedTopics'].value;
}

onTopicsChange(event: Event): void {
const target = event.target as HTMLSelectElement;
const selectedOptions = target.selectedOptions;
}


createDescriptionAndImageGroup(): FormGroup {
return this.formBuilder.group({
'description': [null],
Expand All @@ -57,7 +69,25 @@ export class CreatePostComponent implements OnInit {
}

onSubmit() {
// Handle form submission logic here
this.router.navigate(['/']);
if (this.myForm.valid) {
const post: Post = {
title: this.myForm.get('title')?.value,
topicStrings: this.myForm.get('selectedTopics')?.value,
items: this.myForm.get('descriptionsAndImages')?.value
};

console.log('items', this.myForm.get('descriptionsAndImages')?.value);

this.postService.createPost(post).subscribe(
(response) => {
console.log('Post creado con éxito:', response);
this.router.navigate(['/']);
},
(error) => {
console.error('Error al crear el post:', error);
}
);
}
}

}
8 changes: 0 additions & 8 deletions front/src/app/post/post.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ import { BrowserModule } from '@angular/platform-browser';
@NgModule({
declarations: [PostComponent, CommentComponent, TopComponent, FeedComponent, CreatePostComponent],
imports: [BrowserModule, HttpClientModule,FormsModule, CommonModule, SharedModule, ReactiveFormsModule],
declarations: [
PostComponent,
CommentComponent,
TopComponent,
FeedComponent,
CreatePostComponent,
],
imports: [CommonModule],
exports: [FeedComponent, TopComponent],
})
export class PostModule {}
17 changes: 17 additions & 0 deletions front/src/app/post/services/post.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Post } from '../interfaces/post.interface';

@Injectable({
providedIn: 'root'
})
export class PostService {
private apiUrl = '/api/posts/';

constructor(private http: HttpClient) { }

createPost(post: Post): Observable<Post> {
return this.http.post<Post>(this.apiUrl, post);
}
}
2 changes: 1 addition & 1 deletion front/src/app/post/services/topic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class TopicsService {
constructor(private http: HttpClient) {}

getTopics(){
return this.http.get<Topic[]>(BASE_URL+'/').pipe(
return this.http.get<Topic[]>(BASE_URL).pipe(
map((topics: Topic[]) => {
return topics.map((topic) => {
return {
Expand Down

0 comments on commit 31d7f6e

Please sign in to comment.