using System; using System.Collections; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Net; using System.Web; using System.Web.Http; using System.Web.Http.Description; using DevExpress.Office.Utils; using iBemsDataService.Model; using iBemsDataService.Util; using System.Transactions; using System.Diagnostics; namespace iBemsDataService.Controllers { public class BemsSitePriceController : ApiController { private iBemsEntities db = new iBemsEntities(); [ResponseType(typeof(void))] [ActionName("Insert")] public IHttpActionResult PostBemsSitePriceInsert(SitePrice SitePrice) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { // 그룹을 추가하면서 메뉴들을 그룹에 맞는 권한 테이블로 모두 옮겨 놓는다. using (var transaction = new TransactionScope()) { var newBemsSitePrice = db.BemsSitePrice.Add(new BemsSitePrice { SiteID = SitePrice.SiteId, FuelTypeId = SitePrice.FuelTypeId, PriceTypeId = SitePrice.PriceTypeId, PriceCode = SitePrice.PriceCode, UseYN = SitePrice.UseYN }); db.SaveChanges(); /* foreach (var user in Menu.MenuUsers) { //menu.SiteId = newUserGroup.SiteId; //menu.UserGroupId = newUserGroup.UserGroupId; //db.CmUserGroupPermission.Add( menu ); //Trace.WriteLine( string.Format( "{0}-{1}" , newUserGroup.UserGroupId , menu.MenuId ) ); db.BemsSitePriceUser.Add( new BemsSitePriceUser { SiteId = newMenu.SiteId, MenuId = newMenu.MenuId, UserId = user.UserId, } ); db.SaveChanges(); }*/ transaction.Complete(); } } catch (Exception) { throw; } return StatusCode(HttpStatusCode.NoContent); } [ResponseType(typeof(void))] [ActionName("InsertAll")] public IHttpActionResult PostBemsSitePriceInsertAll(List SitePrice) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { using (var transaction = new TransactionScope()) { foreach (var m in SitePrice) { var newM = db.BemsSitePrice.Add(new BemsSitePrice { SiteID = m.SiteId, FuelTypeId = m.FuelTypeId, PriceTypeId = m.PriceTypeId, PriceCode = m.PriceCode, UseYN = m.UseYN }); } db.SaveChanges(); transaction.Complete(); } } catch (Exception) { throw; } return StatusCode(HttpStatusCode.NoContent); } [ResponseType(typeof(void))] [ActionName("Delete")] public IHttpActionResult PostBemsSitePriceDelete(SitePrice SitePrice) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { using (var transaction = new TransactionScope()) { var foundQuery = db.BemsSitePrice.Where(x => x.SiteID == SitePrice.SiteId && x.FuelTypeId == SitePrice.FuelTypeId && x.PriceTypeId == SitePrice.PriceTypeId && x.PriceCode == SitePrice.PriceCode); db.BemsSitePrice.Remove(foundQuery.FirstOrDefault()); //한개만 지운다 //new QueryManager(db).Delete(db.BemsSitePrice, foundQuery);//여러개 db.SaveChanges(); transaction.Complete(); } } catch (Exception) { throw; } return StatusCode(HttpStatusCode.NoContent); } [ResponseType(typeof(void))] [ActionName("DeleteAll")] public IHttpActionResult PostBemsSitePriceDeleteAll(SitePrice SitePrice) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { using (var transaction = new TransactionScope()) { var foundQuery = db.BemsSitePrice.Where(x => x.SiteID == SitePrice.SiteId && x.PriceTypeId == SitePrice.PriceTypeId); //db.BemsSitePrice.Remove(foundQuery.FirstOrDefault()); 한개만 지운다 new QueryManager(db).Delete(db.BemsSitePrice, foundQuery); db.SaveChanges(); transaction.Complete(); } } catch (Exception) { throw; } return StatusCode(HttpStatusCode.NoContent); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } private bool BemsSitePriceExists(int id) { return db.BemsSitePrice.Count(e => e.SiteID == id) > 0; } public class SitePrice { public int SiteId { get; set; } public Int16 FuelTypeId { get; set; } public int PriceTypeId { get; set; } public string PriceCode { get; set; } public string UseYN { get; set; } }; } }