/// <summary> /// DELETE /api/shippers/{id} /// </summary> /// <param name="id"></param> /// <param name="data"></param> /// <returns></returns> public HttpResponseMessage Delete(int id, ShipperModel model) { var context = this.DbContext; var entity = context.Shippers.Find(id); if (entity == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } if (!this.User.CanDelete(entity)) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)); } // create the web event var webEvent = new ShipperDeletedEvent(entity); // delete the entity context.Shippers.Remove(entity); // persist changes to the database context.SaveChanges(); // fire the web event webEvent.Raise(); return(new HttpResponseMessage(HttpStatusCode.NoContent)); }
/// <summary> /// PUT /api/shippers/{id} /// </summary> /// <param name="id"></param> /// <param name="data"></param> /// <returns></returns> public ShipperModel Put(int id, ShipperModel model) { var context = this.DbContext; var entity = context.Shippers.Find(id); if (entity == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } if (!this.User.CanUpdate(entity)) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)); } // update the entity entity.UpdateFrom(model); // persist changes to the database context.SaveChanges(); // fire the web event new ShipperUpdatedEvent(entity).Raise(); return(selector(entity)); }
/// <summary> /// POST /api/shippers /// </summary> /// <param name="data"></param> /// <returns></returns> public HttpResponseMessage Post(ShipperModel model) { var context = this.DbContext; if (!this.User.CanCreate <Shipper>()) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)); } // transform the ShipperModel to Shipper var entity = model.TransformTo <Shipper>(); // add the entity context.Shippers.Add(entity); // persist changes to the database context.SaveChanges(); // fire the web event new ShipperCreatedEvent(entity).Raise(); // create response var response = Request.CreateResponse <ShipperModel>(HttpStatusCode.Created, selector(entity)); string uri = Url.Link("Api", new { id = entity.Id }); response.Headers.Location = new Uri(uri); return(response); }
public async Task TestMethod_測試InsertShipper() { var ShipperID = new ShipperModel { CompanyName = "Fragile", Phone = "(503) 555-9000" }; var result = await shipperService.InsertShipper(ShipperID); Assert.AreEqual(result, 1); }
public async Task FailedToSaveNewShipper_AddShipper_ReturnBadRequest() { var shipperModel = new ShipperModel(); var response = await _shipperController.AddShipper(shipperModel); Assert.IsType <ActionResult <ShipperModel> >(response); Assert.IsType <BadRequestResult>(response.Result); _shipperService.Verify(sh => sh.Add(It.IsAny <Shipper>())); }
public async Task IdOfNonExistentShipper_UpdateShipper_ShipperNotUpdated() { const int shipperId = -1; var shipperModel = new ShipperModel(); var response = await _shipperController.UpdateShipper(shipperId, shipperModel); Assert.IsType <ActionResult <ShipperModel> >(response); Assert.IsType <NotFoundResult>(response.Result); }
public async Task <int> UpdateShipper(ShipperModel model) { string sql = @"UPDATE [Northwind].[dbo].[Shippers] SET [CompanyName] = @CompanyName, [Phone] = @Phone WHERE [ShipperID] = @ShipperID"; var parameters = new DynamicParameters(model); var result = await _dataProvider.ExecuteNonQueryAsync(sql, CommandType.Text, parameters); return(result); }
public async Task <int> InsertShipper(ShipperModel model) { string sql = @"INSERT INTO [Northwind].[dbo].[Shippers] ([CompanyName],[Phone]) VALUES (@CompanyName, @Phone) "; var parameters = new DynamicParameters(model); var result = await _dataProvider.ExecuteNonQueryAsync(sql, CommandType.Text, parameters); return(result); }
// GET: Administration/Shipper/Edit/5 public async Task <ActionResult> Edit(Int32 id) { var entity = await Task.Run(() => { return(Uow.ShipperRepository.Get(new Shipper(id))); }); var model = new ShipperModel(entity); return(View(model)); }
public IHttpActionResult UpdateShipper(ShipperModel existing) { var entity = new Shipper { CompanyName = existing.CompanyName, Phone = existing.Phone }; _repository.UpdateShipper(entity); return(Ok <ShipperModel>(existing)); }
/// <summary> /// Create a new instance of <see cref="Shipper" /> from a <see cref="ShipperModel" /> /// </summary> /// <typeparam name="T">The type of instance to create</typeparam> /// <param name="source">The object to be transformed</param> /// <returns></returns> public static Shipper TransformTo <T>(this ShipperModel source) where T : Shipper { // create a new Shipper var target = new Shipper(); // update the Shipper with source target.UpdateFrom(source); // return updated target return(target); }
public async Task <IActionResult> Post([FromBody] ShipperModel Model) { var result = await _shippderService.InsertShipper(Model); if (result > 0) { return(StatusCode((int)HttpStatusCode.Created)); } else { return(BadRequest()); } }
public async Task <IActionResult> Put(int id, [FromBody] ShipperModel Model) { Model.ShipperID = id; var result = await _shippderService.UpdateShipper(Model); if (result > 0) { return(Ok()); } else { return(BadRequest()); } }
// GET: Admin/Shipper public ActionResult Index() { if (Session["Account"] == null) { return(RedirectToAction("Index", "Login", new { area = "" })); } else { Session["URL"] = null; ShipperModel shipper = new ShipperModel(); ViewBag.Shipper = shipper.getShipper(); return(View()); } }
public IHttpActionResult CreateShipper(ShipperModel newShipper) { var entity = new Shipper { CompanyName = newShipper.CompanyName, Phone = newShipper.Phone }; entity = _repository.CreateShipper(entity); newShipper.ShipperID = entity.ShipperID; String loc = Url.Link("api/shippers/{shipperID}", new { shipperID = entity.ShipperID }); return(Created <ShipperModel>(loc, newShipper)); }
public async Task TestMethod_測試UpdateShipper() { var ShipperID = await GetMaxId(); var RandomTel = DateTime.Now.ToString("hhmmss"); var Shipper = new ShipperModel { ShipperID = ShipperID, CompanyName = "Fragile", Phone = RandomTel }; var result = await shipperService.UpdateShipper(Shipper); var GetResult = await shipperService.GetShipperByID(ShipperID); Assert.AreEqual(GetResult.FirstOrDefault().Phone, RandomTel); }
//[TokenAuthorization("admin")] public IHttpActionResult Put(int id, ShipperModel model) { try { Shipper shipper = Factory.Create(model); UnitOfWork.Shippers.Update(shipper, id); UnitOfWork.Commit(); return(Ok(Factory.Create(shipper))); } catch (Exception ex) { Helper.Log(ex.Message, "ERROR"); return(BadRequest(ex.Message)); } }
public async Task ShipperModel_AddShipper_ReturnModelWithLocationInHeader() { var shipperModel = new ShipperModel(); _shipperService.Setup(sh => sh.IsSavedToDb()).ReturnsAsync(true); var response = await _shipperController.AddShipper(shipperModel); Assert.IsType <ActionResult <ShipperModel> >(response); var createdAtActionResult = Assert.IsType <CreatedAtActionResult>(response.Result); Assert.IsType <ShipperModel>(createdAtActionResult.Value); Assert.Equal(StatusCodes.Status201Created, createdAtActionResult.StatusCode); Assert.Single(createdAtActionResult.RouteValues.Keys, "shipperId"); }
//[TokenAuthorization("admin")] public IHttpActionResult Post(ShipperModel model) { try { Shipper shipper = Factory.Create(model); // stvaranje novog shippera UnitOfWork.Shippers.Insert(shipper); UnitOfWork.Commit(); return(Ok(Factory.Create(shipper))); } catch (Exception ex) { Helper.Log(ex.Message, "ERROR"); return(BadRequest(ex.Message)); } }
public void ShipperModelWithShipperId_ShipperModelToShipper_ReturnShipperWithIdZero() { var shipperModel = new ShipperModel { ShipperId = 34, CompanyName = "Test shipper model", Phone = "416-901-5430" }; var shipper = _mapper.Map <Shipper>(shipperModel); Assert.Equal(0, shipper.ShipperId); Assert.Equal("Test shipper model", shipper.CompanyName); Assert.Equal("416-901-5430", shipper.Phone); }
public async Task <ActionResult <ShipperModel> > AddShipper(ShipperModel shipperModel) { var shipper = _mapper.Map <Shipper>(shipperModel); _shipperService.Add(shipper); if (await _shipperService.IsSavedToDb()) { var persistedShipperModel = _mapper.Map <ShipperModel>(shipper); return(CreatedAtAction(nameof(GetShipper), new { shipperId = persistedShipperModel.ShipperId }, persistedShipperModel)); } return(BadRequest()); }
public ActionResult Insert(SHIPPER entity, string PassW, string rePassW) { if (Session["Account"] == null) { return(RedirectToAction("Index", "Login", new { area = "" })); } else { if (Session["URL"] == null) { Session["URL"] = HttpContext.Request.UrlReferrer.AbsoluteUri.ToString(); ViewBag.URL = Session["URL"]; } else { ViewBag.URL = Session["URL"]; } if (!new AccountModel().CheckExist(entity.SDT)) { if (PassW == rePassW) { ShipperModel shipper = new ShipperModel(); if (shipper.insertShipper(entity, PassW)) { TempData["Alert-Message"] = "Thêm Shipper thành công"; TempData["AlertType"] = "alert-success"; } else { TempData["Alert-Message"] = "Thêm Shipper thất bại"; TempData["AlertType"] = "alert-danger"; } } else { TempData["Alert-Message"] = "Xác thực mật khẩu không đúng"; TempData["AlertType"] = "alert-danger"; } } else { TempData["Alert-Message"] = "Số điện thoại đã tồn tại"; TempData["AlertType"] = "alert-danger"; } return(View(entity)); } }
public async Task <ActionResult> Create(ShipperModel model) { try { var entity = new Shipper(); entity.CompanyName = model.CompanyName; entity.Phone = model.Phone; Uow.ShipperRepository.Add(entity); await Uow.CommitChangesAsync(); return(RedirectToAction("Index")); } catch { return(View()); } }
public IHttpActionResult Post([FromBody] ShipperModel model) { try { Shipper shipper = Factory.Create(model); UnitOfWork.Shippers.Insert(shipper); UnitOfWork.Commit(); return(Ok(Factory.Create(shipper))); } catch (DbEntityValidationException ex) { Logger.Log(ex.Message); return(BadRequest(ErrorGeneratorMessage.Generate(ex))); } catch (Exception ex) { Logger.Log(ex.Message, "ERROR"); return(BadRequest(ex.Message)); } }
public async Task <ActionResult> Edit(Int32 id, ShipperModel model) { try { var entity = await Task.Run(() => { return(Uow.ShipperRepository.Get(new Shipper(id))); }); entity.CompanyName = model.CompanyName; entity.Phone = model.Phone; await Uow.CommitChangesAsync(); return(RedirectToAction("Index")); } catch { return(View()); } }
public ShipperServiceTest() { var shipperModel = new ShipperModel { CompanyName = "name", Id = "id", Phone = "phone", ShipperId = 1 }; _shipperRepository = new Mock<IMongoReadOnlyRepository<ShipperModel>>(); _shipperRepository.Setup(x => x.FirstOrDefault(It.IsAny<Expression<Func<ShipperModel, bool>>>())) .Returns(shipperModel); _shipperRepository.Setup(x => x.GetMany(It.IsAny<int>(), It.IsAny<int>(), null, null, It.IsAny<SortDirection>(), It.IsAny<Expression<Func<ShipperModel, object>>[]>())) .Returns(new List<ShipperModel> { shipperModel }); _shipperService = new ShipperService(_shipperRepository.Object); }
public ActionResult Info(string SDT) { if (Session["Account"] == null) { return(RedirectToAction("Index", "Login", new { area = "" })); } else { if (Session["URL"] == null) { Session["URL"] = HttpContext.Request.UrlReferrer.AbsoluteUri.ToString(); ViewBag.URL = Session["URL"]; } else { ViewBag.URL = Session["URL"]; } ShipperModel shipper = new ShipperModel(); ViewBag.Shipper = shipper.getInfoShipper(SDT); return(View()); } }
public ActionResult Info(SHIPPER entity) { if (Session["Account"] == null) { return(RedirectToAction("Index", "Login", new { area = "" })); } else { ShipperModel shipper = new ShipperModel(); if (shipper.updateShipepr(entity)) { TempData["Alert-Message"] = "Chỉnh sửa thông tin Shipper thành công"; TempData["AlertType"] = "alert-success"; return(RedirectToAction("Info", new { SDT = entity.SDT })); } else { TempData["Alert-Message"] = "Chỉnh sửa thông tin Shipper thất bại"; TempData["AlertType"] = "alert-danger"; } return(View(entity)); } }
public static Shipper ToEntity(this ShipperModel model) { return(AutoMapperConfiguration.Mapper.Map <ShipperModel, Shipper>(model)); }
protected void loadData() { ShipperParser newParser = new ShipperParser(); this.dataModel = new ShipperModel(@".\SQL2008", 1433, "TSQLFundamentals2008", "sa", "123456", "Sales.Shippers", newParser); newParser.DataModel = this.dataModel; try { this.dataModel.resetModel(""); } catch (Exception ex) { Session["current_error"] = ex.Message; Response.Redirect("serverError.aspx"); } /*if (this.IsPostBack == false) this.loadEmpIDS();*/ if ((Request.Params.Get("suppid") != null)) { this.suppID = int.Parse(Request.Params.Get("suppid").Trim()); this.newEmpMode = false; if (this.IsPostBack == true) return; this.loadSuppData(); } }
protected void loadData() { string currentFilter ; if (IsPostBack == false) { Session["cat_filter"] = "deactive=0 "; currentFilter = "deactive=0 "; } else currentFilter = (string)Session["cat_filter"]; //this.scriptLb.Text = currentFilter; ShipperParser newParser = new ShipperParser(); this._dataModel = new ShipperModel(this.gvShippers, @".\SQL2008", 1433, "TSQLFundamentals2008","sa", "123456", "Sales.Shippers", newParser); newParser.DataModel = this._dataModel; try { this._dataModel.resetControl(currentFilter); //if (this.IsPostBack == false) // this.loadEmpIDS(); } catch(Exception ex) { Session["current_error"] = ex.Message; Response.Redirect("serverError.aspx"); } }
public EditShipperForm(ShipperModel DataModel) { InitializeComponent(); this.dataModel = DataModel; }
public static Shipper ToEntity(this ShipperModel model, Shipper destination) { return(AutoMapperConfiguration.Mapper.Map(model, destination)); }