web api存储库/valuecontroller问题

r7xajy2e  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(236)

我开发web应用程序。我从实体框架的webapi开始。我要实现积垢功能。阅读功能工作正常,但与创建,更新,删除问题,你能检查它,告诉我,我做错了什么?
我附加了两块代码,我不知道如何在控制器中实现第一个存储库(c,u,d函数中的错误)。
我对webapi没有足够的经验,你能告诉我,我需要如何设置存储库(仅用于实现创建、删除、更新)文件,以及在value controller中实现后如何设置吗
我需要你的建议,关于创建,更新,删除功能的客户回购

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WebAPI
{
    public class CustomerRepository
    {
        public IQueryable<Customer> GetAllCustomers()
        {
            DevelopersEntities dev = new DevelopersEntities();
            return dev.Customers;
        }

        public IQueryable<Customer> GetAllCustomers(int id)
        {
            DevelopersEntities dev = new DevelopersEntities();
            return dev.Customers.Where(c=>c.Id==id).Select(e=>e);
        }

        public IQueryable<Customer> DeleteCustomer(int id)
        {
            DevelopersEntities dev = new DevelopersEntities();
            var cus = dev.Customers.Where(s => s.Id == id).FirstOrDefault();
            dev.Entry(cus).State = System.Data.Entity.EntityState.Deleted;
            dev.SaveChanges();
            return cus;
        }

        }
        public IQueryable<Customer> CreateCustomer(Customer customer)
        {
            DevelopersEntities dev = new DevelopersEntities();
            dev.Customers.Add(new Customer()
             {
                 Id = customer.Id,
                Name = customer.Name
            });
            dev.SaveChanges();
            return Ok();

        }

        public IQueryable<Customer> UpdateCustomer(Customer customer)
        {
            DevelopersEntities dev = new DevelopersEntities();
            var cus = dev.Customers.Where(s => s.Id == customer.Id).FirstOrDefault();
            cus.Id = customer.Id;
            cus.Name = customer.Name;
            dev.SaveChanges();
            return Ok();
        }
    }

值控制器

using DevelopersWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPI;

namespace DevelopersWeb.Controllers
{
    public class ValuesController : ApiController
    {
        ModelFactory _modelFactory;

        public ValuesController()
        {
            _modelFactory = new ModelFactory();
        }
        // GET api/values
        public IEnumerable<CustomerModel> Get()
        {
            CustomerRepository cr = new CustomerRepository();
            return cr.GetAllCustomers().ToList().Select(c=> _modelFactory.Create(c));
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "xxx";
        }

        // POST api/values
        public void Post([FromBody] CustomerModel customerModel)
        {
            CustomerRepository cr = new CustomerRepository();
            cr.CreateCusomer(customer);

            return Ok();
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题