Skip to content

Commit

Permalink
Updated routes to refer to new db tables, and use the viewModel properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Centerville1 committed Nov 10, 2024
1 parent ed836d2 commit e0258b6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Gordon360/Controllers/LostAndFoundController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public ActionResult<FoundItems> GetFoundItem(int itemID)
/// <param name="id">The id</param>
[HttpGet]
[Route("missingitemsbyid/{id}")]
public ActionResult<Missing> GetMissingItem(int id)
public ActionResult<MissingItemReportViewModel> GetMissingItem(int id)
{
Missing? result = lostAndFoundService.GetMissingItem(id);
MissingItemReportViewModel? result = lostAndFoundService.GetMissingItem(id);
if (result != null)
{
return Ok(result);
Expand Down
17 changes: 11 additions & 6 deletions Gordon360/Models/ViewModels/MissingItemReportViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class MissingItemReportViewModel
{
public int? recordID { get; set; }

public string firstName { get; set; }
public string? firstName { get; set; }

public string lastName { get; set; }
public string? lastName { get; set; }

public string category { get; set; }

Expand All @@ -26,18 +26,22 @@ public class MissingItemReportViewModel

public string? stolenDescription { get; set; }

public DateTime? dateLost { get; set; }
public DateTime dateLost { get; set; }

public DateTime? dateCreated { get; set; }
public DateTime dateCreated { get; set; }

public string phone { get; set; }
public string? phone { get; set; }

public string email { get; set; }
public string? email { get; set; }

public string status { get; set; }

public string submitterUsername { get; set; }

public string? submitterID { get; set; }

public bool forGuest { get; set; }

public static implicit operator MissingItemReportViewModel(CCT.MissingItemData MissingReportDBModel) => new MissingItemReportViewModel
{
recordID = MissingReportDBModel.ID,
Expand All @@ -55,6 +59,7 @@ public class MissingItemReportViewModel
email = MissingReportDBModel.email,
status = MissingReportDBModel.status,
submitterUsername = MissingReportDBModel.submitterUsername,
forGuest = MissingReportDBModel.forGuest,
};
}
}
81 changes: 55 additions & 26 deletions Gordon360/Services/LostAndFoundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Gordon360.Models.CCT;
using Gordon360.Models.CCT.Context;
using Gordon360.Models.ViewModels;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -13,57 +14,84 @@ public class LostAndFoundService(CCTContext context) : ILostAndFoundService
{
public int CreateMissingItemReport(MissingItemReportViewModel reportDetails)
{
var newReportResults = context.Missing.Add(new Missing {
firstName = reportDetails.firstName,
lastName = reportDetails.lastName,

var account = context.ACCOUNT.FirstOrDefault(x => x.AD_Username == reportDetails.submitterUsername);

string idNum;

if (account != null)
{
idNum = account.gordon_id;
}
else
{
throw new ResourceCreationException() { ExceptionMessage = "No account could be found for the user." };
}

var newReportResults = context.MissingReports.Add(new MissingReports
{
submitterID = idNum,
forGuest = reportDetails.forGuest,
category = reportDetails.category,
brand = reportDetails.brand,
colors = reportDetails.colors,
brand = reportDetails.brand,
description = reportDetails.description,
locationLost = reportDetails.locationLost,
stolen = reportDetails.stolen,
stolenDescription = reportDetails.stolenDescription,
dateLost = reportDetails.dateLost,
dateCreated = reportDetails.dateCreated,
phoneNumber = reportDetails.phone,
emailAddr = reportDetails.email,
locationLost = reportDetails.locationLost,
stolen = reportDetails.stolen,
stolenDesc = reportDetails.stolenDescription,
dateLost = reportDetails.dateLost,
dateCreated = reportDetails.dateCreated,
status = reportDetails.status,
adminUsername = reportDetails.submitterUsername });
// var newReportResults = context.Missing.Add(new Missing(reportDetails))
});

context.SaveChanges();

if (newReportResults?.Entity == null || newReportResults?.Entity?.recordID == 0)
if (newReportResults == null || newReportResults?.Entity?.ID == 0)
{
throw new ResourceCreationException() { ExceptionMessage = "The report could not be saved." };
}

if (reportDetails.forGuest)
{
throw new ResourceCreationException() { ExceptionMessage = "The application could not be saved." };
var newGuest = context.GuestUsers.Add(new GuestUsers
{
missingID = newReportResults.Entity.ID,
firstName = reportDetails.firstName,
lastName = reportDetails.lastName,
phoneNumber = reportDetails.phone,
emailAddress = reportDetails.email,
});

context.SaveChanges();

if (newGuest.Entity == null || newReportResults?.Entity?.ID == 0)
{
throw new ResourceCreationException() { ExceptionMessage = "The user associated with this record could not be saved." };
}
}

// Retrieve the application ID number of this new application
int reportID = newReportResults.Entity.recordID;
int reportID = newReportResults.Entity.ID;

return reportID;
}

/// <param name="id">The id</param>
public async Task UpdateMissingItemReportAsync(int id, MissingItemReportViewModel reportDetails)
{
var original = await context.Missing.FindAsync(id);
var original = await context.MissingReports.FindAsync(id);

if (original != null)
{
original.firstName = reportDetails.firstName;
original.lastName = reportDetails.lastName;
original.category = reportDetails.category;
original.brand = reportDetails.brand;
original.description = reportDetails.description;
original.locationLost = reportDetails.locationLost;
original.stolen = reportDetails.stolen;
original.stolenDescription = reportDetails.stolenDescription;
original.stolenDesc = reportDetails.stolenDescription;
original.dateLost = reportDetails.dateLost;
original.dateCreated = reportDetails.dateCreated;
original.phoneNumber = reportDetails.phone;
original.emailAddr = reportDetails.email;
original.status = reportDetails.status;
original.adminUsername = reportDetails.submitterUsername;

await context.SaveChangesAsync();

Expand All @@ -77,7 +105,7 @@ public async Task UpdateMissingItemReportAsync(int id, MissingItemReportViewMode
/// <param name="id">The id</param>
public async Task UpdateReportStatusAsync(int id, string status)
{
var original = await context.Missing.FindAsync(id);
var original = await context.MissingReports.FindAsync(id);

if (original != null)
{
Expand Down Expand Up @@ -126,9 +154,10 @@ public IEnumerable<FoundItems> GetFoundItems()
/// </summary>
/// <param name="id">The ID of the missing item</param>
/// <returns>A Missing, or null if no item matches the id</returns>
public Missing? GetMissingItem(int id)
public MissingItemReportViewModel? GetMissingItem(int id)
{
return context.Missing.FirstOrDefault(x => x.recordID == id);
MissingItemData report = context.MissingItemData.FirstOrDefault(x => x.ID == id);
return (MissingItemReportViewModel)report;
}
}
}
2 changes: 1 addition & 1 deletion Gordon360/Services/ServiceInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public interface ILostAndFoundService
Task UpdateMissingItemReportAsync(int id, MissingItemReportViewModel reportDetails);
Task UpdateReportStatusAsync(int id, string status);
FoundItems? GetFoundItem(int ID);
Missing? GetMissingItem(int id);
MissingItemReportViewModel? GetMissingItem(int id);
}


Expand Down

0 comments on commit e0258b6

Please sign in to comment.