示例#1
0
        // POST api/StyleInfoAPI
        public HttpResponseMessage PostStyle(Style style)
        {
            if (style.BrandId == 0 || string.IsNullOrEmpty(style.PatternName) || string.IsNullOrEmpty(style.MarketingDescription))
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "All fields except Tech Bullets are required.");

            if (ModelState.IsValid)
            {
                var style2 = db.Styles.SingleOrDefault(s => s.StockNumber == style.StockNumber);
                if (style2 != null)
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Style number already exists. Please use update.");

                var pattern = db.Patterns.SingleOrDefault(p => p.PatternName == style.PatternName);
                if (pattern == null) pattern = new Pattern();
                pattern.BrandId = style.BrandId;
                pattern.PatternName = style.PatternName;
                pattern.LastUpdated = DateTime.Now;
                db.Entry(pattern).State = pattern.PatternId == 0 ? EntityState.Added : EntityState.Modified;
                db.SaveChanges(); // Need to save the pattern first to get an id

                style.Pattern = pattern;
                db.Styles.Add(style);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, style);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = style.StyleId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
示例#2
0
        public ActionResult Index(PIMModel model, HttpPostedFileBase file)
        {
            model.Brands = GetBrands();

            if (ModelState.IsValid)
            {
                int added = 0;
                int updated = 0;

                if (file != null && file.ContentLength > 0 && file.FileName.Contains("xls"))
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Files/upload"), fileName);
                    file.SaveAs(path);
                    var dt = ReadSpreadsheetIntoDataTable(path, "ProductInfo");
                    System.IO.File.Delete(path);

                    using (var db = new PIMContext())
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            if (string.IsNullOrEmpty(row["Style Number"].ToString().Trim()))
                            {
                                ModelState.AddModelError("StyleError", "A style number is missing. Please correct and re-upload.");
                                return View(model);
                            }
                            string patternName = row["Pattern Name"].ToString().Trim();
                            var pattern = db.Patterns.SingleOrDefault(p => p.PatternName == patternName);
                            if (pattern == null) pattern = new Pattern();
                            pattern.BrandId = model.BrandId;
                            pattern.PatternName = patternName;
                            pattern.LastUpdated = DateTime.Now;
                            // Add if it doesn't exist otherwise just updates the existing pattern that was loaded
                            db.Entry(pattern).State = pattern.PatternId == 0 ? EntityState.Added : EntityState.Modified;
                            db.SaveChanges(); // Need to save the pattern first to get an id

                            string styleNumber = row["Style Number"].ToString().Trim();
                            var style = db.Styles.SingleOrDefault(s => s.StockNumber == styleNumber);
                            if (style == null) style = new Style();
                            style.PatternId = pattern.PatternId;
                            style.StockNumber = styleNumber;
                            style.MarketingDescription = row["Marketing Description"].ToString();
                            style.TechBullets = !string.IsNullOrEmpty(row["Tech Bullets"].ToString()) ?
                                row["Tech Bullets"].ToString() : null;
                            style.LastUpdated = DateTime.Now;
                            style.LastUpdatedBy = User.Identity.Name;

                            db.Entry(style).State = style.StyleId == 0 ? EntityState.Added : EntityState.Modified;
                            if (style.StyleId == 0)
                                added++;
                            else
                                updated++;
                            db.SaveChanges();
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("XlsError", "Please upload a valid Excel file.");
                    return View(model);
                }

                return RedirectToAction("ViewProducts", new { b = model.BrandId, a = added, u = updated });
            }

            return View(model);
        }
示例#3
0
        // PUT api/StyleInfoAPI/5
        public HttpResponseMessage PutStyle(int id, Style style)
        {
            if (style.BrandId == 0 || string.IsNullOrEmpty(style.PatternName) || string.IsNullOrEmpty(style.MarketingDescription))
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "All fields except Tech Bullets are required.");

            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != style.StyleId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            var pattern = db.Patterns.SingleOrDefault(p => p.PatternName == style.PatternName);
            style.PatternId = pattern.PatternId;
            db.Entry(style).State = EntityState.Modified;
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }