public OutletOut CreateOutlet(OutletIn outletIn) { var outlet = MapHelper.Map <OutletIn, Outlet>(outletIn); var user = ffContext.Users.FirstOrDefault(usr => usr.Id == outletIn.UserId); if (user == null || !user.Enabled) { throw new ArgumentException("userId"); } outlet.User = user; ffContext.Outlets.Add(outlet); if (outlet.Image != null) { FileHelper.SaveFormFile(outletIn.Image, environment.WebRootPath + "\\Images", outlet.Id + ".png"); outlet.Image = outlet.Id + ".png"; } var workingHours = MapHelper .Map <List <WorkingHours>, WorkingHoursIn, WorkingHours>(outletIn.WorkingHours); workingHours.ForEach(wh => { wh.Outlet = outlet; }); ffContext.AddRange(workingHours); ffContext.SaveChanges(); return(GetOutlet(outlet.Id)); }
public IActionResult Update([FromBody] OutletIn outlet, string id) { try { var outletOut = outletService.UpdateOutlet(id, outlet); return(Json(outletOut)); } catch (ArgumentException ex) { Response.StatusCode = 403; var msg = new JsonMessage("Исключение на свойстве: " + ex.Message, ""); return(Json(msg)); } }
public IActionResult Create(OutletIn outlet) { try { var outletOut = outletService.CreateOutlet(outlet); return(Json(outletOut)); } catch (ArgumentException ex) { Response.StatusCode = 403; var msg = new JsonMessage("Исключение на свойстве: " + ex.Message, ""); return(Json(msg)); } }
public OutletOut UpdateOutlet(string outletId, OutletIn outletIn) { var outlet = MapHelper.Map <OutletIn, Outlet>(outletIn); outlet.Id = outletId; var user = ffContext.Users.FirstOrDefault(usr => usr.Id == outletIn.UserId); if (user == null || !user.Enabled) { throw new ArgumentException("userId"); } outlet.User = user; if (outletIn.Image != null) { outlet.Image = outlet.Id + ".png"; FileHelper.SaveFormFile(outletIn.Image, environment.WebRootPath + "\\Images", outlet.Id + ".png"); } ffContext.Outlets.Update(outlet); ffContext.Entry(outlet).Property(olt => olt.Image).IsModified = outlet.Image != null; var workingHoursDeleted = ffContext.WorkingHours.Where(wh => wh.Outlet.Id == outlet.Id); ffContext.WorkingHours.RemoveRange(workingHoursDeleted); ffContext.SaveChanges(); var workingHours = MapHelper .Map <List <WorkingHours>, WorkingHoursIn, WorkingHours>(outletIn.WorkingHours); workingHours.ForEach(wh => { wh.Outlet = outlet; }); ffContext.AddRange(workingHours); ffContext.SaveChanges(); ffContext.Entry(outlet).Reload(); return(GetOutlet(outlet.Id)); }