small fixes & added url shortener
This commit is contained in:
1 parent
f683b4e34b
commit
94c87b933f
8 files changed
+398
-1
No files matched your search
@@ -61,7 +61,6 @@ export class AdminComponent implements OnInit {
|
||||
return;
|
||||
}
|
||||
this.loadArticles();
|
||||
this.loadArticles();
|
||||
}
|
||||
|
||||
navigateToLogin() {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 }
|
||||
];
|
||||
@@ -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<ShortURL[]> {
|
||||
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
|
||||
return this.http.get<ShortURL[]>(`${this.apiUrl}?timestamp=${new Date().getTime()}`, {headers});
|
||||
}
|
||||
|
||||
postURL(token: string, og_url: string, short_name?: string): Observable<any[]> {
|
||||
return this.http.post<any[]>(this.apiUrl, {
|
||||
og_url,
|
||||
short_name
|
||||
},{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
deleteURL(id: number, token: string): Observable<boolean> {
|
||||
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
|
||||
return this.http.delete<any>(`${this.apiUrl}/${id}`, { headers }).pipe(
|
||||
map((response) => {
|
||||
return !!(response && response.message === 'Short URL deleted');
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<div class="container-wrapper">
|
||||
<div class="container">
|
||||
<h1>URL Shortener</h1>
|
||||
|
||||
<!-- URL Creation Form -->
|
||||
<div class="form-section">
|
||||
<form (ngSubmit)="createShortURL(shortURLForm)" #shortURLForm="ngForm">
|
||||
<div class="form-group">
|
||||
<input
|
||||
type="url"
|
||||
id="og_url"
|
||||
name="og_url"
|
||||
ngModel
|
||||
class="form-control"
|
||||
placeholder="Original URL"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<input
|
||||
type="text"
|
||||
id="short_name"
|
||||
name="short_name"
|
||||
ngModel
|
||||
class="form-control"
|
||||
placeholder="Short URL Name (Optional)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Shorten URL</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Delete URL Section -->
|
||||
<div class="form-section">
|
||||
<h3>Delete Short URL</h3>
|
||||
<form (ngSubmit)="deleteShortURL(deleteURLForm)" #deleteURLForm="ngForm">
|
||||
<div class="form-group">
|
||||
<input
|
||||
type="number"
|
||||
id="deleteId"
|
||||
name="deleteId"
|
||||
ngModel
|
||||
class="form-control"
|
||||
placeholder="ID"
|
||||
required
|
||||
/>
|
||||
<button type="submit" class="btn btn-danger">Delete</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- List of Short URLs -->
|
||||
<div class="url-list">
|
||||
<h3>Existing Short URLs</h3>
|
||||
<div *ngIf="noURLs; else urlList">
|
||||
<p>No short URLs available.</p>
|
||||
</div>
|
||||
|
||||
<ng-template #urlList>
|
||||
<ul class="url-list-items">
|
||||
<li *ngFor="let url of urls">
|
||||
<strong>ID {{url.id}}</strong> // <strong>URL: </strong> <a [href]="'https://itz.sh/' + url.short_url" target="_blank">{{ 'https://itz.sh/' + url.short_url }}</a><br>
|
||||
<strong>Original: </strong> <a [href]="url.og_url" target="_blank">{{ (url.og_url | slice:0:45) + (url.og_url.length > 40 ? '...' : '')}}</a><br>
|
||||
</li>
|
||||
</ul>
|
||||
</ng-template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -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<any> = {
|
||||
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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user