Trying to delete from database using HTTPDELETE does nothing

DS Assignment

I am learning to code in c# and I am developing an API application. My GET method works but I have issues with my DELETE, it returns a success code(200) but it does not delete from my database which is connected to my application. I am using the unit of work and repository patterns and my code is as follows:

Controller code:

        private readonly IOrderService _orderService;

        public OrdersController(IOrderService orderService)
        {
            _orderService = orderService;         
        }

[HttpDelete("{id}")]
        public async Task<ActionResult> RemoveOrder(int id)
        {
            try
            {
                await _orderService.Delete(id);
                return StatusCode(200);
            }
            catch (Exception ex)
            {
                return StatusCode(500);
            }

        }

Service Interface

public interface IOrderService
    {
        Task<Order> Get(int id);
        Task Add(Order order);
        Task Delete(int id);
        Task Update(int id, Order order);

        Task<IEnumerable<Order>> GetAllOrdersAsync();
        Task<IEnumerable<OrderDTO>> GetOrdersToCityAsync(string cityName);

        Task<OrderDTO> GetEmployeeOrdersToCountryAsync
    (
    string countryName, string employeeLastName
    );
    }

Service class:

 public class OrderService : IOrderService
    {
        private readonly IUnitOfWork _unitOfWork;
        public OrderService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
        public async Task Delete(int id)
        {
            try
            {
                var order = await _unitOfWork.OrderRepository.Get(id);
                _unitOfWork.OrderRepository.Delete(order);
                await _unitOfWork.CommitAsync();
            }
            catch (Exception e)
            {
                await _unitOfWork.RollbackAsync();
                throw;
            }
            
        }
}

Unit of work class:

public class UnitOfWork : IUnitOfWork
    {
        private readonly NorthwindContext _db;
        private List<object> _customRepositories = new List<object>();
        private IProductRepository _productRepository;
        private IOrderRepository _orderRepository;
        public UnitOfWork(
            NorthwindContext db, 
            IProductRepository ProductRepository,
            IOrderRepository orderRepository
            )
        { 
            _db = db;
            _productRepository = ProductRepository;
            _orderRepository = orderRepository;
        }

uow interface:

    public interface IUnitOfWork
    {
        IProductRepository ProductRepository { get; }

        IOrderRepository OrderRepository { get; }
        Task CommitAsync();
        Task RollbackAsync();
    }


Order repository interface which extends my genericRepository:

    public interface IOrderRepository : IGenericRepository<Order>
    {
        Task<IEnumerable<OrderDTO>> GetOrdersToCityAsync(string cityName);

        Task<OrderDTO> GetEmployeeOrdersToCountryAsync
            (
            string countryName, string employeeLastName
            );
    }

Order repository:

   public class OrderRepository : GenericRepository<Order>, IOrderRepository
    {
        private readonly NorthwindContext _northwindContext;
        public OrderRepository(NorthwindContext db) : base(db)
        {
            _northwindContext = db;
        }

generic repository:

    public class GenericRepository<T> : IGenericRepository<T> where T : class
    {
        protected readonly NorthwindContext _db;

        public GenericRepository(NorthwindContext db)
        {
            _db = db;
        }


        public void Delete(T entity)
        {
            _db.Set<T>().Remove(entity);
        }

Please ignore if the curly braces are not closed properly, in my application they are.

Daniel Efrén

Please check the following items:

  • Unit of work implementation calls SaveChanges() on CommitAsync implementation.
  • NorthwindContext object instance passed to repositories is the same instance as the one injected into UnitOfWork class. Otherwise you'll be working in different transactions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am trying to delete a record in the database using mysql javascript API

Alert Message Does not Show in screen while a record delete from Database

Delete record from database using AJAX

Delete image from database using ajax and php

Spring CrudRepository delete() does nothing

Does deleting data from a database (using actual delete SQL query) cause huge problems in re-indexing of table data?

Action from notification does nothing

Trying to loop through, and delete from, a list of tables using SSIS

Error trying to open a csv using FileSystemObject: code runs and does nothing

Delete record by using ajax from database EntityFramework

I am trying to add products to database using mysqli and php but nothing gets added

Firebase Realtime Database, setValue() does nothing

Delete from database using javascript

Trying to load data dynamically from the database in a from using PDO

apt-get dist-upgrade does nothing when trying to upgrade from wheezy to jessie

Delete Rows from MySQL Database Using Checkboxes

Delete row using datatable plugin then delete it from database

Trying to make HTML table from MySQL database; PHP doing nothing

Trying to delete potential outliers in a huge database using R.

DisplayFor helper does not populate records from database in delete view

Getting a error when trying to delete a column from my database using Vue Client

trying to delete from an API using axios and React hooks

When does room delete entities from database?

(Django) Delete function does not remove data from database

Delete data from database using php and sql

Trying to delete Row from table using react.js

ASP.NET Core 5.0 Web API throws error 405 when trying to delete record using HttpDelete

I am deleting the object from my database using delete button made in angular 13, but this does not delete it instantly from frontend

Incorrect number of binding when trying to delete from a database (Tkinter, SQLite)