demo20230512/Service/WeeeFabDataService.cs

56 lines
1.6 KiB
C#
Raw Permalink Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using Weee.DAL;
using Weee.Models;
namespace Weee.Service
{
public class WeeeFabDataService : WeeeDataAuthorizeService
{
public WeeeFabDataService(WeeeDataContext db) : base(db)
{
}
public DbContextTransaction BeginTransaction()
{
var ret = _db.Database.BeginTransaction();
return ret;
}
//public int SaveChanges()
//{
// return _db.SaveChanges();
//}
public Fab GetFab(int id)
{
return _db.Fabs.Where(x => x.ID == id).First();
}
public IEnumerable<Fab> GetFabs()
{
IEnumerable<Fab> ret;
if (loggedInUserCompanyAdmin)
ret = _db.Fabs.Where(x => x.CompanyID == CurrentCompany.ID).OrderByDescending(x => x.ID).ToList();
else
ret = _db.Fabs.Where(x => x.UserId == loggedInUserId).OrderByDescending(x => x.ID).ToList();
return ret;
}
public Fab SaveFab(Fab ToBeSave)
{
ToBeSave.CompanyID = CurrentCompany.ID;
var entry = _db.Entry(ToBeSave);
if (ToBeSave.ID == 0)
entry.State = EntityState.Added;
else if (AuthorizedFabs.Contains(ToBeSave.ID))
{
entry.State = EntityState.Modified;
entry.Property(x => x.CompanyID).IsModified = false;
}
_db.SaveChanges();
return ToBeSave;
}
}
}