Пример #1
0
        public IActionResult EditTraining(TrainingViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //get training
            Training training = _trainingService.GetTrainingWithId(model.Id).FirstOrDefault();

            if (training == null)
            {
                ModelState.AddModelError("Error", "No such data!");
                return(View(model));
            }

            training.Title           = model.Title.ToCapital();
            training.TrainingPlaceId = model.PlaceId;
            training.DateTime        = model.Date.Add(model.Time.TimeOfDay);
            training.Description     = model.Description;
            training.Distance        = model.Distance;
            training.ModifiedDate    = DateTime.UtcNow;

            _trainingService.UpdateTraining(training);

            //delete old add new photo if any provided; else do not bother.
            if (model.Photo != null)
            {
                string fileName    = model.Photo.FileName;
                string contentType = model.Photo.ContentType;

                Stream       stream  = model.Photo.OpenReadStream();
                BinaryReader reader  = new BinaryReader(stream);
                byte[]       content = reader.ReadBytes((int)model.Photo.Length);

                //delete existing training photo
                TrainingPhoto photo = _trainingPhotoService.GetPhotoWithForeignKey(model.Id);
                if (photo != null)
                {
                    _trainingPhotoService.DeletePhoto(photo);
                }

                //add new
                photo = new TrainingPhoto
                {
                    Name         = model.Photo.FileName,
                    ContentType  = model.Photo.ContentType,
                    Content      = content,
                    AddedDate    = DateTime.UtcNow,
                    ModifiedDate = DateTime.UtcNow,
                    TrainingId   = model.Id
                };
                _trainingPhotoService.InsertPhoto(photo);
            }

            _trainingService.SaveTrainingChanges();
            _trainingPhotoService.SavePhotoChanges();

            return(RedirectToAction("Trainings"));
        }
Пример #2
0
        public IActionResult DownloadTrainingPhoto(long trainingId)
        {
            TrainingPhoto photo = _trainingPhotoService.GetPhotoWithForeignKey(trainingId);

            if (photo == null)
            {
                return(View("Error", new ErrorViewModel {
                    ErrorMessage = "Not Found!"
                }));
            }

            return(File(photo.Content, photo.ContentType, photo.Name));
        }
Пример #3
0
        public IActionResult AddTraining(TrainingViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //add training
            Training training = new Training
            {
                Title           = model.Title.ToCapital(),
                TrainingPlaceId = model.PlaceId,
                DateTime        = model.Date.Add(model.Time.TimeOfDay),
                Description     = model.Description,
                Distance        = model.Distance,
                AddedDate       = DateTime.UtcNow,
                ModifiedDate    = DateTime.UtcNow
            };

            long trainingId = _trainingService.InsertTraining(training);

            //add related image if any; else do not bother now.
            //TODO: Later give an error
            if (model.Photo != null)
            {
                string fileName    = model.Photo.FileName;
                string contentType = model.Photo.ContentType;

                Stream       stream  = model.Photo.OpenReadStream();
                BinaryReader reader  = new BinaryReader(stream);
                byte[]       content = reader.ReadBytes((int)model.Photo.Length);

                TrainingPhoto photo = new TrainingPhoto
                {
                    Name         = model.Photo.FileName,
                    ContentType  = model.Photo.ContentType,
                    Content      = content,
                    AddedDate    = DateTime.UtcNow,
                    ModifiedDate = DateTime.UtcNow,
                    TrainingId   = trainingId
                };

                _trainingPhotoService.InsertPhoto(photo);
            }

            _trainingService.SaveTrainingChanges();
            _trainingPhotoService.SavePhotoChanges();

            return(RedirectToAction("Trainings"));
        }