Exemplo n.º 1
0
        public async Task <ActionResult> UpdateOsoblje(int id, [FromBody] OsobljeUpdateModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var item = await _userService.UpdateOsoblje(id, model);

                    return(Ok(item));
                }
                else
                {
                    return(BadRequest(model));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new ApiException(ex.Message, System.Net.HttpStatusCode.BadRequest)));
            }
        }
        public async Task <OsobljeModel> UpdateOsoblje(int id, OsobljeUpdateModel model)
        {
            try
            {
                var uposlenik = _context.Uposlenik
                                .Include(u => u.ApplicationUser)
                                .ThenInclude(au => au.ApplicationUserRoles)
                                .ThenInclude(aur => aur.Role)
                                .Include(u => u.ApplicationUser)
                                .ThenInclude(au => au.Opcina)
                                .Where(u => u.ApplicationUserId == id)
                                .FirstOrDefault();

                uposlenik.ApplicationUser.Active        = model.Active;
                uposlenik.ApplicationUser.DatumRodjenja = model.DatumRodjenja;
                uposlenik.DatumZaposlenja       = model.DatumZaposlenja;
                uposlenik.ApplicationUser.Email = model.Email;
                uposlenik.ApplicationUser.Ime   = model.Ime;
                uposlenik.ApplicationUser.JMBG  = model.JMBG;
                if (uposlenik.ApplicationUser.OpcinaId != model.OpcinaId)
                {
                    uposlenik.ApplicationUser.OpcinaId = model.OpcinaId;
                    uposlenik.ApplicationUser.Opcina   = _context.Opcina.Find(model.OpcinaId);
                }
                uposlenik.ApplicationUser.Prezime = model.Prezime;
                if (model.Slika != null && model.Slika.Length > 0)
                {
                    uposlenik.ApplicationUser.Slika = model.Slika;
                }
                uposlenik.ApplicationUser.Spol = model.Spol;

                //new password if not empty
                if (!string.IsNullOrEmpty(model.Password) && !string.IsNullOrEmpty(model.PasswordConfirm))
                {
                    if (model.Password != model.PasswordConfirm)
                    {
                        throw new Exception("Passwords are not a match.");
                    }
                    uposlenik.ApplicationUser.PasswordHash = UserAuthHelpers.GenerateHash(uposlenik.ApplicationUser.PasswordSalt, model.Password);
                }

                //removing all roles
                var existingRoles = await _context.ApplicationUserRole
                                    .Where(a => a.ApplicationUserId == uposlenik.ApplicationUser.Id)
                                    .ToListAsync();

                foreach (var er in existingRoles)
                {
                    _context.ApplicationUserRole.Remove(er);
                }
                //adding updated roles
                foreach (var r in model.ApplicationUserRoles)
                {
                    uposlenik.ApplicationUser.ApplicationUserRoles.Add(new ApplicationUserRole
                    {
                        ApplicationUserId = uposlenik.ApplicationUser.Id,
                        RoleId            = r.Id,
                        Role = _context.Role.Find(r.Id)
                    });
                }

                await _context.SaveChangesAsync();

                return(MapUposlenikToOsobljeModel(uposlenik));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }