public int AddReferencePicture(IFormFile image) { try { var dir = _env.ContentRootPath; string pathToReferencePictures = "/Images/reference_pictures"; string fullPath = dir + pathToReferencePictures; if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } int fileSuffix = 1; string fullFileName = "reference_picture_" + fileSuffix + ".png"; bool exists = System.IO.File.Exists(Path.Combine(fullPath, fullFileName)); while (exists) { fileSuffix++; fullFileName = "reference_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); ReferencePicture referencePicture = new ReferencePicture(); Picture pictureExists = db.Picture.Where(p => p.PictureName == fullFileName).FirstOrDefault(); if (pictureExists == null) { Picture picture = new Picture(0, fullFileName); db.Picture.Add(picture); referencePicture.PictureId = picture.PictureId; referencePicture.Picture = picture; } else { referencePicture.PictureId = pictureExists.PictureId; referencePicture.Picture = pictureExists; } db.ReferencePicture.Add(referencePicture); db.SaveChanges(); return(1); } } catch { throw; } }
public ReferencePicture GetReferencePictureDetails(int id) { try { ReferencePicture referencePicture = db.ReferencePicture.Find(id); return(referencePicture); } catch { throw; } }
public int UpdateReferencePicture(ReferencePicture referencePicture) { try { db.Entry(referencePicture).State = EntityState.Modified; db.SaveChanges(); return(1); } catch { throw; } }