Skip to content

Commit

Permalink
logic changes, endpoint and API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Carpenteri1 committed Feb 1, 2025
1 parent 60afa87 commit f8e9d27
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 23 deletions.
4 changes: 4 additions & 0 deletions Controllers/LocalStorageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public IResult Save([FromBody] ComponentModel newComponent)
public async Task<ComponentModel[]?> Get() =>
await LocalComponentHandler.Get();

[HttpGet("getbyid/{Id}")]
public async Task<ComponentModel?> GetById(int Id) =>
await LocalComponentHandler.GetById(Id);

[HttpPost("edit")]
public IResult Edit([FromBody] ComponentModel component) =>
LocalComponentHandler.Edit(component);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Component, Injectable, OnInit} from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedService } from '../../shared.service';
import {ComponentModel} from "../../Models/Component.Model";
import {HandleComponent} from "../modals/handleComponent/handle.component";
import {AppComponent} from "../../app.component";

Expand Down Expand Up @@ -41,7 +40,5 @@ export class BasicComponent implements OnInit {
Remove(id: number): void {
this.sharedService.RemoveComponent(id);
}

protected readonly ComponentModel = ComponentModel;
protected readonly AppComponent = AppComponent;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="dropdown">
<button type="button" [class]="btnTheme" data-bs-toggle="modal" [attr.data-bs-target]="'#'+modalBindDropdownId+'_'+component.id" (click)="LoadData(component.id)">
<button type="button" [class]="btnTheme" data-bs-toggle="modal" [attr.data-bs-target]="'#'+modalBindDropdownId+'_'+component.id">
<i [class]="btnIcon"></i>
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export class HandleComponent {
wantToUploadIcon = false;
wantToLinkToImage = false;

iconData: IconModel = new IconModel("","","");
@Input() component: ComponentModel | ComponentModel = new ComponentModel(0,"","", this.iconData,"");
iconData= new IconModel("","","");
@Input() component = new ComponentModel(0,"","", this.iconData,"");
@Input() acceptButton!: () => void;

triggerAccept() {
Expand All @@ -42,10 +42,6 @@ export class HandleComponent {
constructor(public sharedService: SharedService){
}

public LoadData(id:number)
{
this.component = this.sharedService.GetComponentById(id);
}
public AddComponent() {
const newId = Math.floor(Math.random() * 100) + 1;
let index = this.sharedService.GetId(newId);
Expand Down
23 changes: 21 additions & 2 deletions Gridly-Client/src/app/shared.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,26 @@ export class SharedService{
return this.flexItems === null ? -1 : this.flexItems.findIndex(item => item.id == id);
}
GetComponentById(id: number): ComponentModel{
this.LoadComponentList();
return this.flexItems.find(item => item.id == id) as ComponentModel;
let component = this.flexItems.find(item => item.id == id) as ComponentModel;
if(component === undefined || component === null){
if(this.flexItems === null ||
this.flexItems.length === 0)
this.isLoading = true;

this.http.get<ComponentModel>(`${this.apiUrl}getbyid/${id}`).subscribe(
(returnData) => {

if(this.isLoading)
this.isLoading = false;

component = returnData;
},
(error) => {
console.error('Error fetching component:', error);
this.isLoading = false;
}
);
}
return component;
}
}
14 changes: 9 additions & 5 deletions Handler/LocalComponentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static IResult Save(ComponentModel newComponent)
!DataStorage.WriteIconToFolder(newComponent.IconData))
Results.StatusCode(500);

var componentModels = DataStorage.ReadFromJsonFile().Result?.ToList();
var componentModels =
DataStorage.ReadAllFromJsonFile().Result?.ToList();

if(!componentModels.Any())
componentModels = new List<ComponentModel>();
Expand All @@ -28,7 +29,7 @@ public static IResult Edit(ComponentModel component)
Results.StatusCode(500);

var componentModels =
DataStorage.ReadFromJsonFile().Result?.ToList();
DataStorage.ReadAllFromJsonFile().Result?.ToList();

if (!componentModels.Any())
componentModels = new List<ComponentModel>();
Expand All @@ -47,12 +48,15 @@ public static IResult Edit(ComponentModel component)
return DataStorage.ReadToJsonFile(componentModels) ?
Results.Ok() : Results.StatusCode(500);
}
public static async Task<ComponentModel[]> Get() =>
await DataStorage.ReadFromJsonFile();
public static async Task<ComponentModel[]?> Get() =>
await DataStorage.ReadAllFromJsonFile();

public static async Task<ComponentModel?> GetById(int Id) =>
await DataStorage.ReadByIdFromJsonFile(Id);

public static IResult Delete(int componentId)
{
var componentModels = DataStorage.ReadFromJsonFile()
var componentModels = DataStorage.ReadAllFromJsonFile()
.Result?.ToList();

if(componentModels is null || !componentModels.Any())
Expand Down
10 changes: 10 additions & 0 deletions Services/DataConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json;
using Gridly.Models;

namespace Gridly.Services;

public class DataConverter
{
public static ComponentModel[]? DeserializeJsonString(string jsonString) =>
JsonSerializer.Deserialize<ComponentModel[]>(jsonString);
}
18 changes: 16 additions & 2 deletions Services/DataStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,33 @@ public static bool ReadToJsonFile(List<ComponentModel> newComponent)
}
}

public static async Task<ComponentModel[]?> ReadFromJsonFile()
public static async Task<ComponentModel[]?> ReadAllFromJsonFile()
{
try
{
string jsonString = await File.ReadAllTextAsync(JsonPath);
return JsonSerializer.Deserialize<ComponentModel[]>(jsonString);
return DataConverter.DeserializeJsonString(jsonString);
}
catch (NullReferenceException e) { Console.WriteLine(e.Message); }
catch(JsonException e) { Console.WriteLine(e.Message); }
catch (Exception e) { Console.WriteLine(e.Message); }

return ComponentModel.EmptyArray;
}

public static async Task<ComponentModel?> ReadByIdFromJsonFile(int Id)
{
try
{
string jsonString = await File.ReadAllTextAsync(JsonPath);
return DataConverter.DeserializeJsonString(jsonString).ToList().First(x => x.Id == Id);
}
catch (NullReferenceException e) { Console.WriteLine(e.Message); }
catch(JsonException e) { Console.WriteLine(e.Message); }
catch (Exception e) { Console.WriteLine(e.Message); }

return null;
}

public static bool WriteIconToFolder(IconModel iconData)
{
Expand Down
8 changes: 4 additions & 4 deletions wwwroot/main.js

Large diffs are not rendered by default.

0 comments on commit f8e9d27

Please sign in to comment.