示例#1
0
        public int AddFabric(Fabric fabric, IFormFile image)
        {
            try
            {
                var dir = _env.ContentRootPath;

                string pathToFabricPictures = "/Images/fabric_pictures";

                string fullPath = dir + pathToFabricPictures;

                if (!Directory.Exists(fullPath))
                {
                    Directory.CreateDirectory(fullPath);
                }

                int fileSuffix = 1;

                string fullFileName = "fabric_picture_" + fileSuffix + ".png";

                bool exists = System.IO.File.Exists(Path.Combine(fullPath, fullFileName));

                while (exists)
                {
                    fileSuffix++;
                    fullFileName = "fabric_picture_" + fileSuffix + ".png";
                    exists       = System.IO.File.Exists(Path.Combine(fullPath, fullFileName));
                }

                using (var fileStream = new FileStream(Path.Combine(fullPath, fullFileName), FileMode.Create, FileAccess.Write))
                {
                    image.CopyTo(fileStream);

                    Fabric  fabricToCreate = new Fabric();
                    Picture pictureExists  = db.Picture
                                             .Where(p => p.PictureName == fullFileName)
                                             .FirstOrDefault();

                    if (pictureExists == null)
                    {
                        Picture picture = new Picture(0, fullFileName);
                        db.Picture.Add(picture);
                        fabric.PictureId = picture.PictureId;
                        fabric.Picture   = picture;
                    }
                    else
                    {
                        fabric.PictureId = pictureExists.PictureId;
                        fabric.Picture   = pictureExists;
                    }
                    db.Fabric.Add(fabric);
                    db.SaveChanges();
                    return(1);
                }
            }
            catch
            {
                throw;
            }
        }
示例#2
0
 public Fabric GetFabricDetails(int id)
 {
     try
     {
         Fabric fabric = db.Fabric.Find(id);
         return(fabric);
     }
     catch
     {
         throw;
     }
 }
示例#3
0
        public int UpdateFabric(Fabric fabric, IFormFile image, string imageChanged)
        {
            try
            {
                if (imageChanged.Equals("false"))
                {
                    Fabric oldFabric = db.Fabric.Find(fabric.FabricId);
                    fabric.PictureId          = oldFabric.PictureId;
                    db.Entry(oldFabric).State = EntityState.Detached;
                    db.Entry(fabric).State    = EntityState.Modified;
                    db.SaveChanges();
                    return(1);
                }
                else
                {
                    var dir = _env.ContentRootPath;

                    string pathToReferencePictures = "/Images/fabric_pictures";

                    string fullPath = dir + pathToReferencePictures;

                    if (!Directory.Exists(fullPath))
                    {
                        Directory.CreateDirectory(fullPath);
                    }

                    int fileSuffix = 1;

                    string fullFileName = "fabric_picture_" + fileSuffix + ".png";

                    bool exists = File.Exists(Path.Combine(fullPath, fullFileName));

                    while (exists)
                    {
                        fileSuffix++;
                        fullFileName = "fabric_picture_" + fileSuffix + ".png";
                        exists       = System.IO.File.Exists(Path.Combine(fullPath, fullFileName));
                    }

                    using (var fileStream = new FileStream(Path.Combine(fullPath, fullFileName), FileMode.Create, FileAccess.Write))
                    {
                        image.CopyTo(fileStream);
                        Picture pictureExists = db.Picture
                                                .Where(p => p.PictureName == fullFileName)
                                                .FirstOrDefault();

                        if (pictureExists == null)
                        {
                            Picture picture = new Picture(0, fullFileName);
                            db.Picture.Add(picture);
                            fabric.PictureId = picture.PictureId;
                            fabric.Picture   = picture;
                        }
                        else
                        {
                            fabric.PictureId = pictureExists.PictureId;
                            fabric.Picture   = pictureExists;
                        }
                        db.Entry(fabric).State = EntityState.Modified;

                        db.SaveChanges();
                        return(1);
                    }
                }
            }
            catch
            {
                throw;
            }
        }