Exemple #1
0
        public HikeVM(HikeDTO row)
        {
            Id         = row.Id;
            Title      = row.Title;
            StartDate  = row.StartDate;
            EndDate    = row.EndDate;
            StartPlace = row.StartPlace;
            EndPlace   = row.EndPlace;
            Distance   = row.Distance;

            this.Files = new List <FileVM>();
            if (row.Files != null && row.Files.Any())
            {
                foreach (FileDTO fileDto in row.Files)
                {
                    this.Files.Add(new FileVM(fileDto));
                }
            }

            History = new List <HistoryHikeVM>();
        }
        public IActionResult Create(HikeVM model, IEnumerable <IFormFile> files)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (_db.Hikes.Any(x => x.Title == model.Title))
            {
                TempData["TitleExist"] = "Wędrówka o takim tytule już istnieje - zmień go";
                return(View(model));
            }

            HikeDTO toAdd = new HikeDTO(model);

            _db.Hikes.Add(toAdd);
            _db.SaveChanges();

            #region Upload Image

            //create structure of directories
            var rootDir   = _environment.ContentRootPath;
            var imagesDir = rootDir + "\\wwwroot\\Hikes\\Files\\";
            var hikeDir   = imagesDir + "\\Hike_" + toAdd.Id;

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

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

            if (files != null && files.Any())
            {
                foreach (IFormFile file in files)
                {
                    //check file extension
                    string ext = file.ContentType.ToLower();
                    if (ext != "image/jpg" &&
                        ext != "image/jpeg" &&
                        ext != "image/pjpeg" &&
                        ext != "image/gif" &&
                        ext != "image/png" &&
                        ext != "image/x-png")
                    {
                        ModelState.AddModelError("", "Obraz nie został przesłany - nieprawidłowe rozszerzenie obrazu.");
                        return(View(model));
                    }

                    using (var fileStream = new FileStream(Path.Combine(hikeDir, file.FileName), FileMode.Create, FileAccess.Write))
                    {
                        file.CopyTo(fileStream);
                    }

                    string displayName = file.FileName;
                    if (file.FileName.Contains("."))
                    {
                        int index = displayName.IndexOf(".");
                        displayName = displayName.Remove(0, index);
                    }


                    //save image name to db
                    FileDTO fileDto = new FileDTO()
                    {
                        FileName    = file.FileName,
                        DisplayName = displayName,
                        FileType    = ext,
                        HikeId      = toAdd.Id,
                        UploadTime  = DateTime.Now
                    };

                    _db.Files.Add(fileDto);
                    _db.SaveChanges();
                }
            }
            #endregion

            return(RedirectToAction("Details", new{ Id = toAdd.Id }));
        }
Exemple #3
0
        public void Update(HikeVM toUpdate)
        {
            if (toUpdate.Id == null || toUpdate.Id == 0)
            {
                return;
            }

            HikeDTO travel = _db.Hikes.Find(toUpdate.Id);

            if (toUpdate.Title != travel.Title)
            {
                Attribute lblAttr = toUpdate.Title.GetType().IsDefined(typeof(DisplayAttribute))? toUpdate.Title.GetType().GetCustomAttribute(typeof(DisplayAttribute)): null;

                string fieldLbl = toUpdate.GetType().GetProperty("Title").Name;

                if (lblAttr != null)
                {
                    fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString();
                }

                _db.HikesHistory.Add(new HistoryHikeDTO()
                {
                    HikeId      = travel.Id,
                    ModifyTime  = DateTime.Now,
                    OldValue    = travel.Title,
                    NewValue    = toUpdate.Title,
                    Description = "Modyfikacja pola " + fieldLbl
                });

                travel.Title = toUpdate.Title;
            }

            if (toUpdate.StartDate != travel.StartDate)
            {
                Attribute lblAttr = toUpdate.StartDate.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.StartDate.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null;

                string fieldLbl = toUpdate.GetType().GetProperty("StartDate").Name;

                if (lblAttr != null)
                {
                    fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString();
                }

                _db.HikesHistory.Add(new HistoryHikeDTO()
                {
                    HikeId      = travel.Id,
                    ModifyTime  = DateTime.Now,
                    OldValue    = travel.StartDate.ToString(),
                    NewValue    = toUpdate.StartDate.ToString(),
                    Description = "Modyfikacja pola " + fieldLbl
                });

                travel.StartDate = toUpdate.StartDate;
            }

            if (toUpdate.EndDate != travel.EndDate)
            {
                Attribute lblAttr = toUpdate.EndDate.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.EndDate.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null;

                string fieldLbl = toUpdate.GetType().GetProperty("EndDate").Name;

                if (lblAttr != null)
                {
                    fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString();
                }

                _db.HikesHistory.Add(new HistoryHikeDTO()
                {
                    HikeId      = travel.Id,
                    ModifyTime  = DateTime.Now,
                    OldValue    = travel.EndDate.ToString(),
                    NewValue    = toUpdate.EndDate.ToString(),
                    Description = "Modyfikacja pola " + fieldLbl
                });

                travel.EndDate = toUpdate.EndDate;
            }

            if (toUpdate.StartPlace != travel.StartPlace)
            {
                Attribute lblAttr = toUpdate.StartPlace.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.StartPlace.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null;

                string fieldLbl = toUpdate.GetType().GetProperty("StartPlace").Name;

                if (lblAttr != null)
                {
                    fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString();
                }

                _db.HikesHistory.Add(new HistoryHikeDTO()
                {
                    HikeId      = travel.Id,
                    ModifyTime  = DateTime.Now,
                    OldValue    = travel.StartPlace,
                    NewValue    = toUpdate.StartPlace,
                    Description = "Modyfikacja pola " + fieldLbl
                });

                travel.StartPlace = toUpdate.StartPlace;
            }

            if (toUpdate.EndPlace != travel.EndPlace)
            {
                Attribute lblAttr = toUpdate.EndPlace.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.EndPlace.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null;

                string fieldLbl = toUpdate.GetType().GetProperty("EndPlace").Name;

                if (lblAttr != null)
                {
                    fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString();
                }

                _db.HikesHistory.Add(new HistoryHikeDTO()
                {
                    HikeId      = travel.Id,
                    ModifyTime  = DateTime.Now,
                    OldValue    = travel.EndPlace,
                    NewValue    = toUpdate.EndPlace,
                    Description = "Modyfikacja pola " + fieldLbl
                });

                travel.EndPlace = toUpdate.EndPlace;
            }

            if (toUpdate.Distance != travel.Distance)
            {
                Attribute lblAttr = toUpdate.Distance.GetType().IsDefined(typeof(DisplayAttribute)) ? toUpdate.Distance.GetType().GetCustomAttribute(typeof(DisplayAttribute)) : null;

                string fieldLbl = toUpdate.GetType().GetProperty("Distance").Name;

                if (lblAttr != null)
                {
                    fieldLbl = lblAttr.GetType().GetProperty("Name").GetValue(lblAttr).ToString();
                }

                _db.HikesHistory.Add(new HistoryHikeDTO()
                {
                    HikeId      = travel.Id,
                    ModifyTime  = DateTime.Now,
                    OldValue    = travel.Distance.ToString(),
                    NewValue    = toUpdate.Distance.ToString(),
                    Description = "Modyfikacja pola " + fieldLbl
                });

                travel.Distance = toUpdate.Distance;
            }

            _db.Update(travel);
            _db.SaveChanges();
        }