Пример #1
0
        public async Task <IActionResult> Create(RewardViewModel rewardVm, IFormFile uploadFile)
        {
            var item = _mapper.Map <Reward>(rewardVm);

            if (item.Id == 0)
            {
                _db.Add(item);
            }
            else
            {
                item.Image = await(from r in _db.Rewards.AsNoTracking()
                                   where r.Id == item.Id
                                   select r.Image).SingleAsync();
                _db.Entry(item).State = EntityState.Modified;
            }

            if (string.IsNullOrEmpty(item.Image) && uploadFile == null || !ModelState.IsValid)
            {
                if (ModelState.IsValid)
                {
                    ModelState.AddModelError("", "Добавьте изображение");
                }

                return(View("Edit", rewardVm));
            }

            await _db.SaveChangesAsync();

            if (uploadFile != null)
            {
                var photoUrl = await CommonActions.CreateImage(_environment, uploadFile, "/imgs/reward/");

                item.Image = photoUrl ?? item.Image;

                _db.Entry(item).State = EntityState.Modified;
                await _db.SaveChangesAsync();
            }

            return(RedirectToAction("Index"));
        }
Пример #2
0
        public async Task <IActionResult> Create(UserViewModel userVM, IFormFile uploadFile)
        {
            if (userVM.Birthdate.Year < DateTime.Today.Year - 150 || DateTime.Today < userVM.Birthdate)
            {
                ModelState.AddModelError("", "Возраст пользователя не может быть больше 150 или быть отрицательным");
            }

            if (!ModelState.IsValid)
            {
                return(View("Edit", userVM));
            }

            var item = _mapper.Map <User>(userVM);

            if (item.Id == 0)
            {
                _db.Add(item);
            }
            else
            {
                item.Photo = await(from u in _db.Users.AsNoTracking()
                                   where u.Id == item.Id
                                   select u.Photo).SingleAsync();
                _db.Entry(item).State = EntityState.Modified;
            }

            await _db.SaveChangesAsync();

            if (uploadFile != null)
            {
                item.Photo = await CommonActions.CreateImage(_environment, uploadFile, "/imgs/user/");

                _db.Entry(item).State = EntityState.Modified;
                await _db.SaveChangesAsync();
            }

            return(RedirectToAction("Index"));
        }