simon-s-99 / games-r-us

A Blazor Web App created by Samuel Lööf, Adam Kumlin, Gabriel Wedin & Simon Sörqvist.
MIT License
3 stars 0 forks source link

9 delete listing #29

Closed gabrielwedin closed 5 months ago

gabrielwedin commented 6 months ago

A delete button to delete a listing based on it's ID.

simon-s-99 commented 5 months ago
<AuthorizeView>
    <Authorized>
        <button @onclick="RemoveListing" class="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600">Delete</button>
    </Authorized>
</AuthorizeView>

@code {
    [Parameter]
    public int ListingID { get; set; }
private void RemoveListing()
    {
        var listingToDelete = dbContext.Listings.Find(ListingID);

        if (listingToDelete != null)
        {
            dbContext.Listings.Remove(listingToDelete);
            dbContext.SaveChanges();
        }
    }
}

I'd say it looks good. However I'd like to see a warning prompt (delete: yes/no) in case the user accidentally presses the "delete button"

@Samuel-Loof make a comment in the code next time for easier readability.

gabrielwedin commented 5 months ago

I did what you asked me to do my friends!

adamkumlin commented 5 months ago

You are comparing a user id to a user name, causing the if statement to never be true. In the following code we got it to work, but it can be done more cleanly with helper methods.

image

simon-s-99 commented 5 months ago

You are comparing a user id to a user name, causing the if statement to never be true. In the following code we got it to work, but it can be done more cleanly with helper methods.

image

The helper methods are located here: image

Specifically this one: image