diff --git a/scripts/prerender-meta.js b/scripts/prerender-meta.js index 817bc35..61afa91 100644 --- a/scripts/prerender-meta.js +++ b/scripts/prerender-meta.js @@ -80,6 +80,13 @@ const staticMeta = { image: 'https://meta.itzminey.dev/assets/ogimg.png', url: 'https://itzminey.dev/imprint', color: '#eb284c' + }, + '/shortener': { + title: 'Shortener | itzMiney', + description: 'URL Shortener', + image: 'https://meta.itzminey.dev/assets/ogimg.png', + url: 'https://itzminey.dev/shortener', + color: '#eb284c' } }; diff --git a/src/app/admin/admin.component.ts b/src/app/admin/admin.component.ts index 17f1bb9..dd30b17 100644 --- a/src/app/admin/admin.component.ts +++ b/src/app/admin/admin.component.ts @@ -61,7 +61,6 @@ export class AdminComponent implements OnInit { return; } this.loadArticles(); - this.loadArticles(); } navigateToLogin() { diff --git a/src/app/app.component.ts b/src/app/app.component.ts index a75662b..511556c 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -41,6 +41,11 @@ export class AppComponent implements OnInit { this.showFooter = false; break; } + case 'shortener': { + this.showNavbar = false; + this.showFooter = false; + break; + } default: { this.showNavbar = true; this.showFooter = true; diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index d968b19..52b09df 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -10,6 +10,7 @@ import {SocialsComponent} from './socials/socials.component'; import {ServicesComponent} from './services/services.component'; import {TosComponent} from './tos/tos.component'; import {ImprintComponent} from './imprint/imprint.component'; +import {ShortenerComponent} from './shortener/shortener.component'; export const routes: Routes = [ { path: '', component: AboutComponent }, @@ -23,5 +24,6 @@ export const routes: Routes = [ { path: 'tos', component: TosComponent }, { path: 'imprint', component: ImprintComponent }, { path: 'blog/:slug', component: ArticlePageComponent }, + { path: 'shortener', component: ShortenerComponent }, { path: '**', pathMatch: 'full', component: NotFoundComponent } ]; diff --git a/src/app/shared/services/shortener.service.ts b/src/app/shared/services/shortener.service.ts new file mode 100644 index 0000000..6d0da11 --- /dev/null +++ b/src/app/shared/services/shortener.service.ts @@ -0,0 +1,49 @@ +import {Injectable} from '@angular/core'; +import {HttpClient, HttpHeaders} from '@angular/common/http'; +import {Observable} from 'rxjs'; +import {map} from 'rxjs/operators'; + +interface ShortURL { + id: number; + og_url: string; + short_url: string; + author_id: string; + custom: boolean, + createdAt: string; + updatedAt: string; +} + +@Injectable({ + providedIn: 'root' +}) +export class ShortenerService { + + private apiUrl = '/api/short'; + + constructor(private http: HttpClient) {} + + getAllURLs(token: string): Observable { + const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`); + return this.http.get(`${this.apiUrl}?timestamp=${new Date().getTime()}`, {headers}); + } + + postURL(token: string, og_url: string, short_name?: string): Observable { + return this.http.post(this.apiUrl, { + og_url, + short_name + },{ + headers: { + 'Authorization': `Bearer ${token}` + } + }); + } + + deleteURL(id: number, token: string): Observable { + const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`); + return this.http.delete(`${this.apiUrl}/${id}`, { headers }).pipe( + map((response) => { + return !!(response && response.message === 'Short URL deleted'); + }) + ); + } +} diff --git a/src/app/shortener/shortener.component.css b/src/app/shortener/shortener.component.css new file mode 100644 index 0000000..5a1e75b --- /dev/null +++ b/src/app/shortener/shortener.component.css @@ -0,0 +1,118 @@ +/* Make the body fill the entire height of the viewport */ +body, html { + height: 100%; + margin: 0; +} + +/* Flexbox to center the container */ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + min-width: 300px; + padding: 25px; + background-color: #ffffff; + border-radius: 35px; + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); + max-height: 80vh; + overflow: scroll; +} + +.container-wrapper { + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + background-color: #592840; +} + +h1 { + margin-top: 60px; + text-align: center; + color: #333; +} + +h3 { + text-align: center; +} + +.form-section { + margin-bottom: 30px; + text-align: center; +} + +.form-group { + display: flex; + margin-bottom: 15px; + gap: 12px; +} + +input[type="url"], +input[type="text"] { + min-width: 240px; + padding: 10px; + border: 1px solid #ddd; + border-radius: 25px; + font-size: 1rem; + box-sizing: border-box; +} + +input[type="number"] { + max-width: 80px; + padding: 10px; + border: 1px solid #ddd; + border-radius: 25px; + font-size: 1rem; + box-sizing: border-box; +} + +button { + padding: 12px 20px; + border: none; + border-radius: 25px; + font-size: 1rem; + cursor: pointer; +} + +.btn-primary { + background-color: #007bff; + color: white; +} + +.btn-danger { + background-color: #dc3545; + color: white; +} + +button:hover { + opacity: 0.9; +} + +/* List styling */ +.url-list { + margin-top: 40px; +} + +.url-list-items { + list-style-type: none; + padding: 0; +} + +.url-list-items li { + background-color: #fff; + padding: 10px 15px; + margin-bottom: 10px; + border: 1px solid #ddd; + border-radius: 20px; + line-height: 1.5rem; +} + +.url-list-items a { + color: #007bff; + text-decoration: none; +} + +.url-list-items a:hover { + text-decoration: underline; +} diff --git a/src/app/shortener/shortener.component.html b/src/app/shortener/shortener.component.html new file mode 100644 index 0000000..22400f6 --- /dev/null +++ b/src/app/shortener/shortener.component.html @@ -0,0 +1,71 @@ +
+
+

URL Shortener

+ + +
+
+
+ +
+ +
+ +
+ + +
+
+ + +
+

Delete Short URL

+
+
+ + +
+
+
+ + +
+

Existing Short URLs

+
+

No short URLs available.

+
+ + + + +
+
+
diff --git a/src/app/shortener/shortener.component.ts b/src/app/shortener/shortener.component.ts new file mode 100644 index 0000000..54b38a8 --- /dev/null +++ b/src/app/shortener/shortener.component.ts @@ -0,0 +1,146 @@ +import { Component, OnInit } from '@angular/core'; +import {Title} from '@angular/platform-browser'; +import {Router} from '@angular/router'; +import {UserService} from '../shared/services/user.service'; +import {ShortenerService} from '../shared/services/shortener.service'; +import {Observer} from 'rxjs'; +import {FormsModule} from '@angular/forms'; +import {NgFor, NgIf, SlicePipe} from '@angular/common'; + +@Component({ + selector: 'app-shortener', + imports: [ + FormsModule, + NgFor, + NgIf, + SlicePipe + ], + templateUrl: './shortener.component.html', + styleUrl: './shortener.component.css' +}) +export class ShortenerComponent implements OnInit { + urls: any[] = []; + noURLs: boolean = false; + + token: string | null = localStorage.getItem('token'); + + og_url: string = ''; + short_name: string | null = null; + deleteId: number | null = null; + + constructor( + private shortenerService: ShortenerService, + private userService: UserService, + private router: Router, + private titleService: Title + ) {} + + ngOnInit() { + this.titleService.setTitle('Shortener | itzMiney') + if (!this.token) { + this.navigateToLogin(); + return; + } + this.loadURLs(); + } + + navigateToLogin() { + this.router.navigate(['/login']).then( + (success) => { + if (success) { + console.log('Navigation to login succeeded'); + } else { + console.warn('Navigation to login failed'); + } + }, + (error) => { + console.error('Navigation error:', error); + } + ); + } + + loadURLs(): void { + if (!this.token) { + this.navigateToLogin(); + return; + } + const urlObserver: Observer = { + next: (data) => { + this.urls = data; + this.noURLs = this.urls.length === 0; + }, + error: (error) => { + console.error('Failed to load Short URLs', error); + }, + complete: () => { + console.log('Short URL loading complete'); + } + }; + if (!this.token) return; + this.shortenerService.getAllURLs(this.token).subscribe(urlObserver); + } + + createShortURL(form: any) { + if (!this.token) { + this.navigateToLogin(); + return; + } + + const og_url = form.value.og_url; + const short_name = form.value.short_name ?? null; + + if (!og_url) { + alert('Original URL Required!'); + return; + } + if (!short_name) { + this.shortenerService.postURL(this.token, og_url).subscribe( + (response) => { + alert('Short URL created successfully!'); + this.loadURLs(); + console.log(response); + }, + (error) => { + alert('Error creating Short URL'); + console.error(error); + } + ); + } else if (short_name) { + this.shortenerService.postURL(this.token, og_url, short_name).subscribe( + (response) => { + alert('Short URL created successfully!'); + this.loadURLs(); + console.log(response); + }, + (error) => { + alert('Error creating Short URL'); + console.error(error); + } + ); + } + } + + deleteShortURL(form: any) { + if (!this.token) { + this.navigateToLogin(); + return; + } + + const id = form.value.deleteId + + if (id && !isNaN(Number(id))) { + this.shortenerService.deleteURL(id, this.token).subscribe( + () => { + alert(`Short URL with ID ${id} deleted successfully`); + this.loadURLs(); + }, + (error) => { + alert(`Error deleting Short URL ${id}: ${error.message}`); + console.error('Error deleting Short URL:', error) + } + ); + } else { + alert('Invalid input. Please enter a valid Short URL ID.'); + } + } +}