public IHttpActionResult Create(UserCouponPackageCM cm) { try { UserCouponPackage model = AutoMapper.Mapper.Map <UserCouponPackageCM, UserCouponPackage>(cm); model.PurchasedDate = DateTime.Now; // Get Current time model.UserId = CURRENT_USER_ID; CouponPackage package = _couponPackageService.GetCouponPackage(model.CouponPackageId); // set user package property model.DrinkQuantity = package.DrinkQuantity; model.Price = package.Price; // Current hour, minute, second int cHour = model.PurchasedDate.Hour; int cMinute = model.PurchasedDate.Minute; int cSecond = model.PurchasedDate.Second; // CREATE 30 coupon item for this user package model.CouponItems = new List <CouponItem>(); for (int i = 0; i < 30; i++) { CouponItem newCouponItem = new CouponItem() { IsUsed = false, DateExpired = model.PurchasedDate.AddDays(i) .AddHours(24 - cHour - 1) .AddMinutes(60 - cMinute - 1) .AddSeconds(60 - cSecond - 1), OrderId = null }; model.CouponItems.Add(newCouponItem); } // CREATE USER PACKAGE _userCouponPackageService.CreateUserCouponPackage(model); _userCouponPackageService.SaveUserCouponPackageChanges(); UserCouponPackageVM result = AutoMapper.Mapper.Map <UserCouponPackage, UserCouponPackageVM>(model); return(Ok(result)); } catch (Exception e) { return(InternalServerError(e)); } }
public IHttpActionResult Create(CouponPackageCM cm) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { CouponPackage couponPackage = AutoMapper.Mapper.Map <CouponPackageCM, CouponPackage>(cm); couponPackage.Picture = null; _couponPackageService.CreateCouponPackage(couponPackage); _couponPackageService.SaveCouponPackageChanges(); if (!String.IsNullOrEmpty(cm.Picture)) { // image stream var bytes = Convert.FromBase64String(cm.Picture); // physical server path string filePath = System.Web.HttpContext.Current.Server.MapPath("~/Media/CouponPackage/"); Guid guid = Guid.NewGuid(); // SAVE IMAGE TO SERVER Image image; using (MemoryStream ms = new MemoryStream(bytes)) { image = Image.FromStream(ms); image.Save(filePath + guid + ".jpg"); } // UPDATE IMAGE PATH couponPackage.Picture = "/Media/CouponPackage/" + guid + ".jpg"; _couponPackageService.UpdateCouponPackage(couponPackage); _couponPackageService.SaveCouponPackageChanges(); } // RESPONSE CouponPackageVM couponPackageVM = AutoMapper.Mapper.Map <CouponPackage, CouponPackageVM>(couponPackage); return(Ok(couponPackageVM)); } catch (Exception e) { return(InternalServerError(e)); } }
public IHttpActionResult Update(CouponPackageUM um) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { CouponPackage updateCouponPackage = AutoMapper.Mapper.Map <CouponPackageUM, CouponPackage>(um); CouponPackage oldCouponPackage = _couponPackageService.GetCouponPackageAsNoTracking(u => u.Id == um.Id); if (!um.Picture.Contains("/Media/CouponPackage/") && !string.IsNullOrEmpty(um.Picture)) { // DELETE OLD AVATAR // physical path to folder contain user avatar string folderPath = System.Web.HttpContext.Current.Server.MapPath("~/Media/CouponPackage/"); // physical path to this user avatar string physicalPath = null; if (!String.IsNullOrEmpty(oldCouponPackage.Picture)) { physicalPath = folderPath + oldCouponPackage.Picture.Substring(oldCouponPackage.Picture.LastIndexOf("/") + 1); } // delete old picture if (File.Exists(physicalPath)) { File.Delete(physicalPath); } // MAPPING NEW PICTURE // new Guid Guid newGuid = Guid.NewGuid(); // image stream var bytes = Convert.FromBase64String(um.Picture); // save image to server Image image; using (MemoryStream ms = new MemoryStream(bytes)) { image = Image.FromStream(ms); image.Save(folderPath + newGuid + ".jpg"); } updateCouponPackage.Picture = "/Media/CouponPackage/" + newGuid + ".jpg"; } else { updateCouponPackage.Picture = oldCouponPackage.Picture; } // UPDATE _couponPackageService.UpdateCouponPackage(updateCouponPackage); _couponPackageService.SaveCouponPackageChanges(); CouponPackageVM result = AutoMapper.Mapper.Map <CouponPackage, CouponPackageVM>(updateCouponPackage); return(Ok(result)); } catch (Exception e) { return(InternalServerError(e)); } }