PositionUs Controller

public IActionResult Create() { return View(); } [HttpPost] public async Task Create(CreatePositionVm vm) { if (!ModelState.IsValid) { return View(); } if (_context.Positions.Any(x => x.PositionName == vm.Name)) { ModelState.AddModelError("", "Eyni adda Position movcuddu!"); return View(); } Position position = new Position() { PositionName = vm.Name, }; await _context.Positions.AddAsync(position); await _context.SaveChangesAsync(); return RedirectToAction("Index"); } public IActionResult Update(int? id) { if (!ModelState.IsValid) { return View(); } if (id == null) { return NotFound(); } var position = _context.Positions.FirstOrDefault(x => x.Id == id); UpdatePositionVm vm = new UpdatePositionVm() { Name = position.PositionName }; if (position == null) { return NotFound(); } return View(vm); } [HttpPost] public async Task Update(UpdatePositionVm vm) { if (!ModelState.IsValid) { return View(); } Position oldPosition = await _context.Positions.FirstOrDefaultAsync(x => x.Id == vm.Id); if (oldPosition == null) { return NotFound(); } oldPosition.Id = vm.Id; oldPosition.PositionName = vm.Name; _context.Positions.Update(oldPosition); _context.SaveChanges(); return RedirectToAction("Index"); } public IActionResult Delete(int? id) { if (!ModelState.IsValid) { return View(); } if (id == null) { return NotFound(); } var position = _context.Positions.FirstOrDefault(y => y.Id == id); if (position == null) { return NotFound(); } _context.Positions.Remove(position); _context.SaveChanges(); return RedirectToAction("Index"); } } public async Task Create() { ViewBag.Positions = await _context.Positions.ToListAsync(); return View(); } [HttpPost] public async Task Create(CreateTeacherVm vm) { ViewBag.Positions = await _context.Positions.ToListAsync(); if (!ModelState.IsValid) { return View(vm); } if (vm.PositionId == null) { if (!await _context.Positions.AnyAsync(x => x.Id == vm.PositionId)) { ModelState.AddModelError("PositionId", $"{vm.PositionId}-Id li position yoxdu!"); return View(); } } Teacher teacher = new Teacher() { Name = vm.Name, Surname = vm.Surname, InstagramUrl = vm.InstagramUrl, FaceBookUrl = vm.FaceBookUrl, TwitterUrl = vm.TwitterUrl, PositionId = vm.PositionId }; if (vm.Photo.Length > 3145728) { ModelState.AddModelError("Photo", "Shekil olcusu 3 mb-dan cox ola bilmez!"); return View(); } if (!vm.Photo.ContentType.Contains("image")) { ModelState.AddModelError("Photo", "Duzgun shekil olcusu daxil edin!"); return View(); } teacher.Img = vm.Photo.FileUpload(_environment.WebRootPath, "Upload/User"); await _context.Teachers.AddAsync(teacher); await _context.SaveChangesAsync(); return RedirectToAction("Index"); } public async Task Update(int? id) { ViewBag.Positions = await _context.Positions.ToListAsync(); var user = await _context.Teachers.Include(x => x.Position).FirstOrDefaultAsync(x => x.Id == id); UpdateTeacherVm vm = new UpdateTeacherVm() { Id = user.Id, Name = user.Name, Surname = user.Surname, InstagramUrl = user.InstagramUrl, TwitterUrl = user.TwitterUrl, FaceBookUrl = user.FaceBookUrl, PositionId = user.PositionId, ImgUrl = user.Img }; return View(vm); } [HttpPost] public async Task Update(UpdateTeacherVm updateTeacherVm) { if (updateTeacherVm == null) { return View("Error"); } Teacher oldTeacher = await _context.Teachers.FirstOrDefaultAsync(s => s.Id == updateTeacherVm.Id); if (!ModelState.IsValid) { ViewBag.Positions = await _context.Positions.ToListAsync(); updateTeacherVm.ImgUrl = oldTeacher.Img; return View(updateTeacherVm); } if (oldTeacher == null) { return View("Error"); } if (updateTeacherVm.NewPhoto != null) { if (!updateTeacherVm.NewPhoto.ContentType.Contains("image")) { ModelState.AddModelError("NewPhoto", "Duzgun shekil olcusu daxil edin"); return View(updateTeacherVm); } if (updateTeacherVm.NewPhoto.Length > 3145728) { ModelState.AddModelError("NewPhoto", "Shekilin olcusu 3 mb-dan az olmalidi"); return View(updateTeacherVm); } if (!string.IsNullOrEmpty(oldTeacher.Img)) { FileExtension.FileDelete(_environment.WebRootPath, "Upload/User", oldTeacher.Img); } oldTeacher.Img = updateTeacherVm.NewPhoto.Fi

Jan 23, 2025 - 03:57
 0
PositionUs Controller
public IActionResult Create()
{
    return View();
}

[HttpPost]
public async Task Create(CreatePositionVm vm)
{
    if (!ModelState.IsValid)
    {
        return View();
    }

    if (_context.Positions.Any(x => x.PositionName == vm.Name))
    {
        ModelState.AddModelError("", "Eyni adda Position movcuddu!");
        return View();
    }

    Position position = new Position()
    {
        PositionName = vm.Name,
    };

    await _context.Positions.AddAsync(position);

    await _context.SaveChangesAsync();

    return RedirectToAction("Index");
}

public IActionResult Update(int? id)
{
    if (!ModelState.IsValid)
    {
        return View();
    }

    if (id == null)
    {
        return NotFound();
    }
    var position = _context.Positions.FirstOrDefault(x => x.Id == id);

    UpdatePositionVm vm = new UpdatePositionVm()
    {

        Name = position.PositionName

    };




    if (position == null)
    {
        return NotFound();
    }

    return View(vm);
}

[HttpPost]
public async Task Update(UpdatePositionVm vm)
{
    if (!ModelState.IsValid)
    {
        return View();
    }

    Position oldPosition = await _context.Positions.FirstOrDefaultAsync(x => x.Id == vm.Id);

    if (oldPosition == null)
    {
        return NotFound();
    }

    oldPosition.Id = vm.Id;
    oldPosition.PositionName = vm.Name;

    _context.Positions.Update(oldPosition);
    _context.SaveChanges();

    return RedirectToAction("Index");
}

public IActionResult Delete(int? id)
{
    if (!ModelState.IsValid)
    {
        return View();
    }

    if (id == null)
    {
        return NotFound();
    }

    var position = _context.Positions.FirstOrDefault(y => y.Id == id);

    if (position == null)
    {
        return NotFound();
    }

    _context.Positions.Remove(position);
    _context.SaveChanges();
    return RedirectToAction("Index");
}

}

public async Task Create()
{
    ViewBag.Positions = await _context.Positions.ToListAsync();
    return View();
}

[HttpPost]
public async Task Create(CreateTeacherVm vm)
{
    ViewBag.Positions = await _context.Positions.ToListAsync();

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

    if (vm.PositionId == null)
    {
        if (!await _context.Positions.AnyAsync(x => x.Id == vm.PositionId))
        {
            ModelState.AddModelError("PositionId", $"{vm.PositionId}-Id li position yoxdu!");
            return View();
        }
    }

    Teacher teacher = new Teacher()
    {
        Name = vm.Name,
        Surname = vm.Surname,
        InstagramUrl = vm.InstagramUrl,
        FaceBookUrl = vm.FaceBookUrl,
        TwitterUrl = vm.TwitterUrl,
        PositionId = vm.PositionId
    };

    if (vm.Photo.Length > 3145728)
    {
        ModelState.AddModelError("Photo", "Shekil olcusu 3 mb-dan cox ola bilmez!");
        return View();
    }

    if (!vm.Photo.ContentType.Contains("image"))
    {
        ModelState.AddModelError("Photo", "Duzgun shekil olcusu daxil edin!");
        return View();
    }

    teacher.Img = vm.Photo.FileUpload(_environment.WebRootPath, "Upload/User");


    await _context.Teachers.AddAsync(teacher);
    await _context.SaveChangesAsync();

    return RedirectToAction("Index");
}

public async Task Update(int? id)
{
    ViewBag.Positions = await _context.Positions.ToListAsync();

    var user = await _context.Teachers.Include(x => x.Position).FirstOrDefaultAsync(x => x.Id == id);


    UpdateTeacherVm vm = new UpdateTeacherVm()
    {
        Id = user.Id,
        Name = user.Name,
        Surname = user.Surname,
        InstagramUrl = user.InstagramUrl,
        TwitterUrl = user.TwitterUrl,
        FaceBookUrl = user.FaceBookUrl,
        PositionId = user.PositionId,
        ImgUrl = user.Img
    };

    return View(vm);
}

[HttpPost]
public async Task Update(UpdateTeacherVm updateTeacherVm)
{

    if (updateTeacherVm == null)
    {
        return View("Error");
    }

    Teacher oldTeacher = await _context.Teachers.FirstOrDefaultAsync(s => s.Id == updateTeacherVm.Id);

    if (!ModelState.IsValid)
    {
        ViewBag.Positions = await _context.Positions.ToListAsync();
        updateTeacherVm.ImgUrl = oldTeacher.Img;
        return View(updateTeacherVm);
    }


    if (oldTeacher == null)
    {
        return View("Error");
    }

    if (updateTeacherVm.NewPhoto != null)
    {
        if (!updateTeacherVm.NewPhoto.ContentType.Contains("image"))
        {
            ModelState.AddModelError("NewPhoto", "Duzgun shekil olcusu daxil edin");
            return View(updateTeacherVm);
        }

        if (updateTeacherVm.NewPhoto.Length > 3145728)
        {
            ModelState.AddModelError("NewPhoto", "Shekilin olcusu 3 mb-dan az olmalidi");
            return View(updateTeacherVm);
        }

        if (!string.IsNullOrEmpty(oldTeacher.Img))
        {
            FileExtension.FileDelete(_environment.WebRootPath, "Upload/User", oldTeacher.Img);
        }

        oldTeacher.Img = updateTeacherVm.NewPhoto.FileUpload(_environment.WebRootPath, "Upload/User");
    }

    oldTeacher.Name = updateTeacherVm.Name;
    oldTeacher.Surname = updateTeacherVm.Surname;

    oldTeacher.InstagramUrl = updateTeacherVm.InstagramUrl;
    oldTeacher.FaceBookUrl = updateTeacherVm.FaceBookUrl;
    oldTeacher.TwitterUrl = updateTeacherVm.TwitterUrl;

    oldTeacher.PositionId = updateTeacherVm.PositionId;

    _context.Teachers.Update(oldTeacher);
    await _context.SaveChangesAsync();

    return RedirectToAction("Index");
}

public IActionResult Delete(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    var user = _context.Teachers.FirstOrDefault(x => x.Id == id);

    if (user == null)
    {
        return NotFound();
    }

    FileExtension.FileDelete(_environment.WebRootPath, "Upload/User", user.Img);

    _context.Teachers.Remove(user);
    _context.SaveChanges();


    return RedirectToAction("Index");
}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

public IActionResult Register()
{
    return View();
}

[HttpPost]
public async Task Register(RegisterVm vm)
{
    if (!ModelState.IsValid)
    {
        return View(vm);
    }

    AppUser user = new AppUser()
    {
        Name = vm.Name,
        Surname = vm.Surname,
        UserName = vm.UserName,
        Email = vm.Email,
    };

    var result = await _userManager.CreateAsync(user, vm.Password);

    if (!result.Succeeded)
    {
        foreach (var item in result.Errors)
        {
            ModelState.AddModelError("", item.Description);
            return View();
        }
    }


    //await _userManager.AddToRoleAsync(user, UserRoles.Admin.ToString());
    await _userManager.AddToRoleAsync(user, UserRoles.Member.ToString());

    return RedirectToAction("Login");
}


public async Task LogOut()
{
    await _signInManager.SignOutAsync();

    return RedirectToAction("Index", "Home");
}


public IActionResult Login()
{
    return View();
}

[HttpPost]
public async Task Login(LoginVm vm, string? ReturnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(vm);
    }

    AppUser user = await _userManager.FindByEmailAsync(vm.EmailOrUsername) ??
        await _userManager.FindByNameAsync(vm.EmailOrUsername);

    if (user == null)
    {
        ModelState.AddModelError("", "EmailOrUsername ve Password yanlisdi!");
        return View();
    }

    var result = await _signInManager.CheckPasswordSignInAsync(user, vm.Password, true);

    if (result.IsLockedOut)
    {
        ModelState.AddModelError("", "Bir muddet sonra tekrar yoxlayin!");
        return View();
    }

    if (!result.Succeeded)
    {
        ModelState.AddModelError("", "EmailOrUsername ve Password yanlisdi!");
        return View();
    }

    await _signInManager.SignInAsync(user, vm.Remember);

    if(ReturnUrl != null)
    {
        return Redirect(ReturnUrl);
    }

    return RedirectToAction("Index", "Home");
}

public async Task CreateRole()
{
    foreach (var item in Enum.GetValues(typeof(UserRoles)))
    {
        await _roleManager.CreateAsync(new IdentityRole()
        {
            Name = item.ToString()
        });
    }

    return RedirectToAction("Index", "Home");
}

}

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow