/// <summary> /// Registe as a new member /// </summary> /// <returns></returns> public IResult RegisteMember(RegisterViewModel register) { IResult result = new Result(); try { if (AccountIsExist(register.Account)) { result.Message = "帳號已被註冊!"; return(result); } var instance = Mapper.Map <RegisterViewModel, Customers>(register); instance.Id = Guid.NewGuid().ToString("N").Substring(0, 25); instance.SignUpTime = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME); instance.Password = HashPassword(instance.Password); instance.Flg = true; // Set account is alive, if user will be suspanded in the future, it can be set false. repository.Create(instance); result.Success = true; } catch (Exception e) { result.Message = e.ToString(); throw; } return(result); }
public IResult AddToCart(string soapId, string c_id) { var result = new Result(); try { if (ItemIsExist(soapId)) { var cart = cartService.GetCart(soapId, c_id); if (cart != null) //檢查是否已加入購物車 { cart.AddCount += 1; repository.Update(cart); } else { var instance = new Carts { SoapId = soapId, CustomerId = c_id, AddCount = 1, AddTime = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME) }; repository.Create(instance); } result.Success = true; return(result); } else { result.Message = "系統錯誤, 請稍後再試"; return(result); } } catch (Exception e) { result.Message = "系統錯誤, 請稍後再試"; return(result); } }
public IResult CheckOut(List <int> Id, string c_id) { IResult result = new Result(); try { LocalDateTimeService timeService = new LocalDateTimeService(); Random r = new Random(); var today = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME); string datetimeStr = today.ToString("yyyyMMddHHmmss"); var orderId = string.Format("{0}{1}", datetimeStr, r.Next(1000, 10000)); var items = db.Carts.Where(x => Id.Contains(x.Id)).ToList(); if (!CheckItemStock(items)) { result.Message = "有商品庫存不足,請下單前確認庫存量是否足夠"; return(result); } var cartsWithSoapList = items.Join(db.Soaps, c => c.SoapId, s => s.Id, (c, s) => new { c, s }).ToList(); var sumPrice = cartsWithSoapList.Sum(x => x.c.AddCount * x.s.Price); var instance = new Orders { AddTime = today, UpdateTime = today, Id = orderId, TotalPrice = sumPrice, StatusId = 1, C_Id = c_id }; db.Orders.Add(instance); bool stockNotEnough = cartsWithSoapList.Where(x => x.s.StockCount - x.c.AddCount < 0).SingleOrDefault() != null; if (stockNotEnough) { result.Message = "有商品庫存不足,請下單前確認庫存量是否足夠"; return(result); } cartsWithSoapList.ForEach(x => x.s.StockCount = x.s.StockCount - x.c.AddCount); items.ForEach(x => x.OrderId = orderId); db.SaveChanges(); result.Success = true; } catch (Exception e) { result.Message = "系統錯誤,請稍後再試"; throw; } return(result); }
public IResult CreateNews(News news) { IResult result = new Result(); try { LocalDateTimeService timeService = new LocalDateTimeService(); var now = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME); news.AddTime = now; news.Id = now.ToString("yyyyMMddHHmmss"); repository.Create(news); result.Success = true; return(result); } catch (Exception e) { result.Message = e.ToString(); return(result); } }
public Soap SaveItem(SoapUploadViewModel model, string serverPath) { var file = model.ImageFile; if (file != null) { var fileName = Path.GetFileName(file.FileName); var extention = Path.GetExtension(file.FileName); var fileNameNoExtention = Path.GetFileNameWithoutExtension(file.FileName); var filePath = serverPath + fileName; file.SaveAs(filePath); var instance = Mapper.Map <SoapUploadViewModel, Soap>(model); instance.Id = Guid.NewGuid().ToString("N").Substring(0, 15); instance.UploadTime = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME); instance.Flg = true; instance.ImageUrl = "/Upload/Soap/" + fileName; repository.Create(instance); return(instance); } return(null); }