public KorisnickiNalog Insert(KorisnickiNalogUpsertRequest request) { if (request.Password != request.PasswordConfirmation) { throw new UserException("Lozinke se ne poklapaju!"); } foreach (var nalog in _context.KorisnickiNalog.ToList()) { if (nalog.Username == request.Username) { throw new UserException("Username se vec koristi!"); } } var entity = _mapper.Map <Database.KorisnickiNalog>(request); entity.PasswordSalt = GenerateSalt(); entity.PasswordHash = GenerateHash(entity.PasswordSalt, request.Password); _context.KorisnickiNalog.Add(entity); _context.SaveChanges(); foreach (var uloga in request.Uloge) { _context.KorisniciUloge.Add(new Database.KorisniciUloge() { DatumIzmjene = DateTime.Now, UlogaId = uloga, KorisnickiNalogId = entity.KorisnickiNalogId }); } _context.SaveChanges(); return(_mapper.Map <Model.KorisnickiNalog>(entity)); }
public KorisnickiNalog Update(int id, KorisnickiNalogUpsertRequest request) { var entity = _context.KorisnickiNalog.Find(id); _context.KorisnickiNalog.Attach(entity); _context.KorisnickiNalog.Update(entity); foreach (var nalog in _context.KorisnickiNalog.ToList()) { if (nalog.Username == request.Username && nalog.Username != nalog.Username) { throw new UserException("Username je zauzet!"); } } if (request.Password != null) { if (request.Password != request.PasswordConfirmation) { throw new UserException("Passwords are not matching"); } entity.PasswordSalt = GenerateSalt(); entity.PasswordHash = GenerateHash(entity.PasswordSalt, request.Password); } entity.Username = request.Username; var entityUloge = _context.KorisniciUloge.Where(c => c.KorisnickiNalogId == entity.KorisnickiNalogId).ToList(); foreach (var postojuceUloge in entityUloge) { _context.KorisniciUloge.Remove(postojuceUloge); } _context.SaveChanges(); foreach (var novaUloga in request.Uloge) { Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge(); korisniciUloge.KorisnickiNalogId = entity.KorisnickiNalogId; korisniciUloge.UlogaId = novaUloga; korisniciUloge.DatumIzmjene = DateTime.Now; _context.KorisniciUloge.Add(korisniciUloge); } _context.SaveChanges(); return(_mapper.Map <Model.KorisnickiNalog>(entity)); }