Ejemplo n.º 1
0
        public CertificationsModel Update(CertificationsModel updateCert)
        {
            var entity = db.Certifications.Attach(updateCert);

            entity.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return(updateCert);
        }
Ejemplo n.º 2
0
        public bool CreateFile(IFormFile file, CertificationsModel cert)
        {
            Fileupload img = new Fileupload();

            img.FileName        = file.FileName;
            img.Author          = User.Identity.Name;
            img.DateAdded       = DateTime.Now;
            img.CertificationId = cert.Id;

            MemoryStream ms = new MemoryStream();

            file.CopyTo(ms);
            img.FileData = ms.ToArray();

            if (fileData.DoesFileExist(img))
            {
                return(false);
            }


            ms.Close();
            ms.Dispose();

            fileData.Add(img);
            fileData.Commit();

            return(true);
        }
Ejemplo n.º 3
0
 public bool IsUsingFile(CertificationsModel cert)
 {
     if (fileData.GetByCertId(cert.Id) == null)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 4
0
 public bool IsUsingCertUrl(CertificationsModel cert)
 {
     if (cert.CertUrl == null)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 5
0
        public CertificationsModel CreateCertification(CertificationsModel newCertification)
        {
            newCertification.Author = User.Identity.Name;
            newCertification.Posted = DateTime.Now;

            var CreatedCertification = certData.Add(newCertification);

            certData.Commit();

            return(CreatedCertification);
        }
Ejemplo n.º 6
0
 public IActionResult DeleteCertification(int?id)
 {
     Certification = certData.GetById(id.Value);
     if (id.HasValue && Certification != null)
     {
         return(View(Certification));
     }
     else
     {
         TempData["Message"] = "This page does not exist!";
         return(RedirectToAction("index", "home"));
     }
 }
Ejemplo n.º 7
0
 public IActionResult UpdateCertifications(int Id)
 {
     Certification = certData.GetById(Id);
     if (Certification.Author == User.Identity.Name)
     {
         ViewData["Function"] = $"Update certification: '{Certification.CertName}'";
         return(View(Certification));
     }
     else
     {
         ViewData["Message"] = $"You are not allowed to edit this certification as you are not the owner";
         return(RedirectToAction("index", "home"));
     }
 }
Ejemplo n.º 8
0
 public CertificationsModel UpdateCertification(CertificationsModel newCertification, int CertificationId)
 {
     //var Certification = certData.GetById(CertificationId);
     //Certification.CertName = newCertification.CertName;
     //Certification.CertSubject = newCertification.CertSubject;
     //Certification.CertSite = newCertification.CertSite;
     //Certification.CertDescription = newCertification.CertDescription;
     //Certification.CertUrl = newCertification.CertUrl;
     TryUpdateModelAsync(newCertification);
     newCertification.Author = User.Identity.Name;
     certData.Update(newCertification);
     certData.Commit();
     TempData["Message"] = $"Certification '{Certification.CertName}' Updated!";
     return(Certification);
 }
Ejemplo n.º 9
0
        public IActionResult CreateCertifications(CertificationsModel newCertification, IFormFile file, int?CertificationId)
        {
            //CHECK MODEL STATE

            Certification = newCertification;
            if (!ModelState.IsValid)
            {
                return(View(Certification));
            }
            if (file == null && Certification.CertUrl != null)
            {
                CreateCertification(Certification);
            }
            else if (file != null && Certification.CertUrl == null)
            {
                if (!IsUsingFileFormatAndSize(file))
                {
                    ModelState.AddModelError("All", "File must be .pdf, .png, jpg or jpeg");
                    ModelState.AddModelError("All", "File must be under 500kb");
                    return(View(Certification));
                }
                var createdCertification = CreateCertification(Certification);
                if (CreateFile(file, createdCertification))
                {
                    return(RedirectToAction("index", "home"));
                }
                else
                {
                    DeleteCertificationNoAuth(createdCertification);
                    TempData["Message"] = "The file uploaded already exists!";
                    return(RedirectToAction("index", "home"));
                }
            }
            else if (file != null && Certification.CertUrl != null)
            {
                ModelState.AddModelError("All", "Cannot use both file upload and certification url to display certificate, please choose one");
                return(View(Certification));
            }
            else
            {
                ModelState.AddModelError("All", "You must upload a certification or add a link to the certification image");
                return(View(Certification));
            }

            TempData["Message"] = $"Created certification '{Certification.CertName}'";
            return(RedirectToAction("index", "home"));
        }
Ejemplo n.º 10
0
        public IActionResult Details(int id)
        {
            Certification = new CertificationsModel();
            Certification = CertData.GetById(id);
            image         = new Fileupload();
            image         = fileData.GetByCertId(id).FirstOrDefault();
            ViewBag.story = JsonConvert.SerializeObject(new String(""));

            if (Certification.CertUrl != null)
            {
                return(View(Certification));
            }

            if (Certification == null)
            {
                TempData["Error"] = $"Certification does not exist";
                return(RedirectToAction("Index", "Certifications"));
            }

            if (Certification.CertUrl == null && image != null)
            {
                System.Console.WriteLine("No Certification url");

                string imageDataBytes = Convert.ToBase64String(image.FileData);

                //IF UPLOADED IS PNG
                if (image.FileName.Contains(".pdf"))
                {
                    ViewBag.story = JsonConvert.SerializeObject(imageDataBytes);
                }
                else
                {
                    string imageUrl = string.Format("data:/image/jpeg;base64,{0}", imageDataBytes);                     //Original
                    Certification.CertUrl = imageUrl;
                }
            }
            else
            {
                TempData["Error"] = $"Certification '{Certification.CertName}' with Id: {Certification.Id} does not have a certification";
                return(RedirectToAction("Index", "Certifications"));
            }

            return(View(Certification));
        }
Ejemplo n.º 11
0
        /*WILL id WORK AS WE DO NOT USE ROUTE HERE?*/
        public IActionResult UpdateCertifications(IFormFile file, CertificationsModel updatedCertification)
        {
            Certification = updatedCertification;

            if (!ModelState.IsValid)
            {
                return(View(Certification));
            }

            if (file == null && Certification.CertUrl != null)
            {
                //update with URL
                if (IsUsingFile(Certification))
                {
                    fileData.Delete(Certification.Id);
                }
                UpdateCertification(Certification, Certification.Id);
                return(RedirectToAction("index", "home"));
            }
            else if (file != null && Certification.CertUrl == null)
            {
                if (!IsUsingFileFormatAndSize(file))
                {
                    ModelState.AddModelError("All", "File must be .pdf, .png, jpg or jpeg");
                    ModelState.AddModelError("All", "File must be under 500kb");
                    /*Return with data intact?*/
                    return(View(Certification));
                }

                if (IsUsingCertUrl(Certification))
                {
                    Certification.CertUrl = null;
                }
                if (IsUsingFile(Certification))
                {
                    fileData.Delete(Certification.Id);
                }
                if (CreateFile(file, Certification))
                {
                    UpdateCertification(Certification, Certification.Id);
                    return(RedirectToAction("index", "home"));
                }
                else
                {
                    TempData["Message"] = "The file uploaded already exists!";
                    return(RedirectToAction("index", "home"));
                }
            }
            else
            {
                if (file != null && Certification.CertUrl != null)
                {
                    ModelState.AddModelError("All", "Cannot use both file upload and certification url to display certificate, please choose one");
                    return(View(Certification));
                }
                else
                {
                    if (IsUsingFile(Certification))
                    {
                        UpdateCertification(Certification, Certification.Id);
                        return(RedirectToAction("index", "home"));
                    }
                    ModelState.AddModelError("All", "You must upload a certification or add a link to the certification image");
                    return(View(Certification));
                }
            }
            TempData["Error"] = "Something wrong happened, please try again";
            return(RedirectToAction("index", "error"));
        }
Ejemplo n.º 12
0
 public void DeleteCertificationNoAuth(CertificationsModel cert)
 {
     certData.Delete(cert.Id);
     certData.Commit();
 }
Ejemplo n.º 13
0
 public CertificationsModel Add(CertificationsModel addCert)
 {
     db.Add(addCert);
     return(addCert);
 }