// CONSTRUCTORS /// <summary> /// Initializes a new instance of <see cref="SingleViewModel"/> /// </summary> /// <param name="user"> /// An instance of <see cref="DataAccess.Entities.User"/> to show /// </param> /// <param name="isEditingEnabled"> /// Determines if entities can be changed /// </param> /// <exception cref="System.ArgumentNullException"> /// Throwns when <paramref name="user"/> is null /// </exception> public SingleViewModel(DataAccess.Entities.User user, bool isEditingEnabled) : base(shownEntity: user, isWritingEnabled: isEditingEnabled) { this.realNickname = user.NickName; this.realAvatarPath = user.MainPhotoPath; this.photos = user.Photos.Select(photo => new DataAccess.Wrappers.PhotoWrapper(photo)).ToArray(); this.selectedPhotoIndex = Core.Configuration.Constants.WRONG_INDEX; // command this.resetAvatarCommand = new Commands.RelayCommand((obj) => { user.MainPhotoPath = null; OnPropertyChanged(nameof(ShownEntity)); }); this.showPhotoCommand = new Commands.RelayCommand(NavigateToPhoto); this.deleteOrUpdateCommand = isEditingEnabled ? (ICommand) new Commands.MultipleCommand(new CommandBase[] { new Commands.Admin.User.Single.ValidateCommand(this), new Commands.Admin.UpdateCommand() }) : (ICommand) new Commands.MultipleCommand(new CommandBase[] { new Commands.Admin.DeleteCommand(), new Commands.Shared.DeletePhotoFolderCommand(((DataAccess.Entities.User)ShownEntity).Id.ToString()), new Commands.Shared.DeleteAvatarFromServerCommand(((DataAccess.Entities.User)ShownEntity).MainPhotoPath) }); Logger.LogAsync(Core.LogMode.Debug, $"Initializes {nameof(SingleViewModel)}"); }
/// <summary> /// Executes command /// </summary> /// <param name="parameter"> /// Command parameters /// </param> public override void Execute(object parameter) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"Execute {nameof(OpenProfileCommand)}"); // gets index int index = openProfileViewModelBase.SelectedUserIndex; if (index == Core.Configuration.Constants.WRONG_INDEX) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"Execute {nameof(OpenProfileCommand)} suspended. Wrong index value {index}"); return; } // gets user DataAccess.Entities.User selectedUser = openProfileViewModelBase.Users[index]; Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"User id {selectedUser.Id}"); // sets new shown user openProfileViewModelBase.DataStorage.ShownUser = selectedUser; // open new window with current user Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"Close all window. Open new Window with current profile"); Services.WindowManager.Instance.SwitchMainWindow( key: nameof(Window.User.MainWindow), viewModel: new ViewModel.User.MainWindowViewModel(), doCloseAllWindow: true); }
private IQuizRepository GetInMemoryQuizRepository() { var serviceProvider = new ServiceCollection().AddLogging().BuildServiceProvider(); var factory = serviceProvider.GetService <ILoggerFactory>(); var _logger = factory.CreateLogger <QuizRepository>(); DbContextOptions <TriviaGameDbContext> options; options = new DbContextOptionsBuilder <TriviaGameDbContext>().UseInMemoryDatabase(databaseName: "TriviaGameDbContext").Options; TriviaGameDbContext _dbContext = new TriviaGameDbContext(options); _dbContext.Database.EnsureDeleted(); DataAccess.Entities.User user = new DataAccess.Entities.User() { UserName = "******", UserId = 3, Email = "*****@*****.**", }; DataAccess.Entities.Category cat1 = new DataAccess.Entities.Category() { Category1 = "Movies", CategoryId = 1, }; DataAccess.Entities.GameMode gm1 = new DataAccess.Entities.GameMode() { GameMode1 = "Normal", GameModeId = 1, }; _dbContext.User.Add(user); _dbContext.Category.Add(cat1); _dbContext.GameMode.Add(gm1); _dbContext.Database.EnsureCreated(); return(new QuizRepository(_dbContext, _logger)); }
public override void Execute(object parameter) { DataAccess.Entities.User user = (DataAccess.Entities.User)parameter; if (!string.IsNullOrEmpty(user.MainPhotoPath)) { System.IO.File.Delete(user.MainPhotoPath); } }
public static DataAccess.Entities.User MapToEntity(this SignUpModel model) { var entity = new DataAccess.Entities.User(); entity.FirstName = model.FirstName; entity.LastName = model.LastName; entity.UserName = model.Email; entity.Email = model.Email; return(entity); }
public async Task <IHttpActionResult> Get(int id) { DataAccess.Entities.User data = _userService.GetById(id); User user = Mapper.Map <DataAccess.Entities.User, User>(data); if (user == null) { return(NotFound()); } return(await Task.FromResult(Ok(user))); }
public async Task <IHttpActionResult> Remove(int id) { DataAccess.Entities.User user = _userService.GetById(id); if (user == null) { return(NotFound()); } _userService.Remove(user); _userService.SaveChanges(); return(await Task.FromResult(Ok())); }
public override void Execute(object parameter) { DataAccess.Entities.User user = (DataAccess.Entities.User)parameter; string directoryToDelete = string.Join(DIRECTORY_SEPARATOR_STR, PHOTOS_SAVE_FOLDER, user.Id); if (System.IO.Directory.Exists(directoryToDelete)) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug | Core.LogMode.Info, $"Delete {directoryToDelete}"); System.IO.Directory.Delete(path: directoryToDelete, recursive: true); } }
public int Create(User user) { var model = new DataAccess.Entities.User { FirstName = user.FirstName, LastName = user.LastName, Email = user.Email }; _dataContext.Users.Add(model); _dataContext.SaveChanges(); return(model.Id); }
public ActionResult CreateUser(DataAccess.Entities.User Model) { Guid NewUserId = new Guid(); try { NewUserId = usersService.CreateUser(Model); } catch (Exception) { throw; } return(Json(NewUserId)); }
/// <summary> /// Adds a new User into the database and updates the log /// </summary> /// <param name="user">The Library model of the User to be added</param> public async Task <int> AddUser(Library.Models.User user) { if (user is null) { throw new ArgumentNullException(nameof(user)); } _logger.LogInformation($"Adding user {user.UserName}"); DataAccess.Entities.User entity = Mapper.Map(user); _dbContext.Add(entity); await _dbContext.SaveChangesAsync(); return(user.UserId); }
public async Task <IHttpActionResult> Update([FromBody] User user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } DataAccess.Entities.User userToUpdate = Mapper.Map <User, DataAccess.Entities.User>(user); _userService.Update(userToUpdate); _userService.SaveChanges(); User userUpdated = Mapper.Map <DataAccess.Entities.User, User>(userToUpdate); return(await Task.FromResult(Ok(userUpdated))); }
public static UserModelItem MapToModel(this DataAccess.Entities.User entity) { var model = new UserModelItem(); model.Id = entity.Id; model.FirstName = entity.FirstName; model.LastName = entity.LastName; model.Email = entity.Email; model.IsLocked = entity.LockoutEnabled; model.Avatar = string.IsNullOrWhiteSpace(entity.Avatar) ? Constants.Constants.Images.DefaultUserAvatar : entity.Avatar; return(model); }
public DataAccess.Entities.User ToDto() { var user = new DataAccess.Entities.User { Id = UserId, Username = Username, Name = Name, LastName = LastName, Email = Email, Phone = Phone, Password = Password }; return(user); }
// METHODS /// <summary> /// Sets filter predicate /// </summary> /// <param name="entity"> /// The entities for which predicate is applied /// </param> /// <returns> /// Boolean values which determines if entity is allowed by predicate or not /// </returns> protected override bool FilterPredicate(object entity) { // gets user DataAccess.Entities.User user = (DataAccess.Entities.User)entity; bool isShown = true; // filtering if (isBlocked != null) { isShown &= user.IsBlocked == isBlocked.Value; } isShown &= DataAccess.Filters.UserFilter.Has(user, nicknameSubstring); return(isShown); }
public async Task <IHttpActionResult> Create([FromBody] User user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } DataAccess.Entities.User userToBeSaved = Mapper.Map <User, DataAccess.Entities.User>(user); _userService.Add(userToBeSaved); _userService.SaveChanges(); User userCreated = Mapper.Map <DataAccess.Entities.User, User>(userToBeSaved); return(await Task.FromResult(Ok(userCreated))); //return Created<User>(Request.RequestUri + userCreated.Id.ToString(), userCreated); }
public UserModel GetUserByUsername(string username) { DataAccess.Entities.User user = _userDao.GetUserByUsername(username); var userModel = new UserModel { UserId = user.Id, Username = user.Username, Name = user.Name, LastName = user.LastName, Email = user.Email, Phone = user.Phone, Password = user.Password }; return(userModel); }
public User ConvertdbUserToUser(DataAccess.Entities.User user) { return(new User() { Id = user.Id, Name = user.Name, Surname = user.Surname, Username = user.Username, IsSupplier = user.IsSupplier, UserType = user.UserType, ConfirmPassword = user.ConfirmPassword, CreatedDate = user.CreatedDate, ModifiedDate = user.ModifiedDate, CreatedUserId = user.CreatedUserId, ModifiedUserId = user.ModifiedUserId, }); }
/// <summary> /// Executes the command /// </summary> /// <param name="parameter"> /// Command parameter /// </param> public override void Execute(object parameter) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"Exetute {nameof(SignUpCommand)}"); // checking if (!registrationViewModel.IsDataValid()) { return; } if (!registrationViewModel.UnitOfWork.UserRepository.IsNicknameFree(registrationViewModel.Nickname)) { registrationViewModel.WindowManager.ShowMessageWindow(Core.Messages.Info.ViewModel.NICKNAME_IS_NOT_AVAILABLE); Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"User can not sign up, because his nickname is not available"); return; } // create user Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "The validation has been passed. Create new user."); DataAccess.Entities.User user = new DataAccess.Entities.User() { NickName = registrationViewModel.Nickname, Password = registrationViewModel.Password }; Core.Logger.GetLogger.LogAsync(Core.LogMode.Info, $"User with nickname {user.NickName} and password {user.Password} has been created"); // add the user Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Add new user."); registrationViewModel.UnitOfWork.UserRepository.Insert(user); // save new user Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Save the user."); registrationViewModel.UnitOfWork.Save(); // sets data registrationViewModel.DataStorage.LoggedUser = user; registrationViewModel.DataStorage.ShownUser = user; // open new window with current user Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "User signed up. Registration window close. Main window opens."); registrationViewModel.WindowManager.SwitchMainWindow( key: nameof(Galagram.Window.User.MainWindow), viewModel: new ViewModel.User.MainWindowViewModel()); }
public static DataAccess.Entities.User MapToEntity(this SignUpModel model, DataAccess.Entities.User entity) { entity.FirstName = string.IsNullOrWhiteSpace(model.FirstName) ? entity.FirstName : model.FirstName; entity.LastName = string.IsNullOrWhiteSpace(model.LastName) ? entity.LastName : model.LastName; entity.Email = string.IsNullOrWhiteSpace(model.Email) ? entity.Email : model.Email; entity.Avatar = string.IsNullOrWhiteSpace(model.Avatar) ? entity.Avatar : model.Avatar; return(entity); }
public void AddUser(UserModel userModel) { DataAccess.Entities.User user = userModel.ToDto(); _userDao.Insert(user); }
/// <summary> /// Executes command /// </summary> /// <param name="parameter"> /// Command parameters /// <para/> /// 1 — bool, is like or dislike /// </param> public override void Execute(object parameter) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"Execute {nameof(LikePhotoCommand)}"); // gets value bool isLike = System.Convert.ToBoolean(parameter); DataAccess.Entities.Photo photo = photoInsideViewModel.Photo; DataAccess.Entities.User user = Services.DataStorage.Instance.LoggedUser; DataAccess.Entities.PhotoLike photoLike = photoInsideViewModel.UnitOfWork.PhotoLikeRepository.TryGetUserLike(photo, user); // suitable for likes and dislikes // STEPS // 1. if like is new -> add it // 2. else if there is like or dislike // 2.1. if click on the same button type -> remove like // 2.2 else if click on the opposite button type -> toggle like Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, isLike ? "Liking" : "Disliking"); if (photoLike == null)// 1. add like { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Add like"); Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Add to data base"); // update data base DataAccess.Context.UnitOfWork.Instance.PhotoLikeRepository.Insert(new DataAccess.Entities.PhotoLike { IsLiked = isLike, Photo = photo, User = user }); // update view Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update View"); if (isLike) { ++photoInsideViewModel.LikeDislikeAmount.LikesAmount; } else { ++photoInsideViewModel.LikeDislikeAmount.DisLikesAmount; } // liked photoInsideViewModel.LikeValue = isLike; } else// 2. there is a like { if (photoLike.IsLiked == isLike)// 2.1 remove like { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Remove like"); Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update valuse in data base"); // updata data base DataAccess.Context.UnitOfWork.Instance.PhotoLikeRepository.Delete(photoLike); DataAccess.Context.UnitOfWork.Instance.PhotoRepository.Update(photo); // update view Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update View"); if (isLike) { --photoInsideViewModel.LikeDislikeAmount.LikesAmount; } else { --photoInsideViewModel.LikeDislikeAmount.DisLikesAmount; } // same button, remove photoInsideViewModel.LikeValue = null; } else// 2.2 toggle dislike to like { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Toggle like"); Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update valuse in data base"); // updata database photoLike.IsLiked = isLike; // toggle DataAccess.Context.UnitOfWork.Instance.PhotoLikeRepository.Update(photoLike); // update view, toggle value Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update View"); if (isLike) { ++photoInsideViewModel.LikeDislikeAmount.LikesAmount; --photoInsideViewModel.LikeDislikeAmount.DisLikesAmount; } else { --photoInsideViewModel.LikeDislikeAmount.LikesAmount; ++photoInsideViewModel.LikeDislikeAmount.DisLikesAmount; } // toggle like photoInsideViewModel.LikeValue = isLike; } } // notify view Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update view. Raise event"); photoInsideViewModel.UpdateLikes(); // save changes to database Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Save changes to data base"); DataAccess.Context.UnitOfWork.Instance.Save(); }
/// <summary> /// Removes a User from the database with a specific ID and updatest the log /// </summary> /// <param name="id">ID of the User to be deleted</param> public void DeleteUser(int id) { _logger.LogInformation($"Deleting user with ID {id}"); DataAccess.Entities.User user = _dbContext.User.Find(id); _dbContext.Remove(user); }
/// <summary> /// Executes the command /// </summary> /// <param name="parameter"> /// Command parameter /// </param> public override void Execute(object parameter) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"Execute {nameof(ValidateCommand)}"); // gets comment DataAccess.Entities.User user = (DataAccess.Entities.User)userSingleViewModel.ShownEntity; // get new user's nickname string userNickname = user.NickName; Core.Logger.GetLogger.LogAsync(Core.LogMode.Info, $"New user's nickname = {userNickname}"); // check if right if (userNickname.Length > Core.Configuration.DBConfig.NICKNAME_MAX_LENGTH || userNickname.Length < Core.Configuration.DBConfig.NICKNAME_MIN_LENGTH) { // interrupt command executing, wrong user's nickname length Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "User's nickname is wrong. Interrupt command executing"); // show message Services.WindowManager.Instance.ShowMessageWindow(Core.Messages.Info.Admin.ADMIN_WRONG_USER_NICKNAME_LENGTH); // interrupt command CommandState = Enums.Admin.CommandState.Interrupted; return; } else if (userSingleViewModel.RealNickname != userNickname && !userSingleViewModel.UnitOfWork.UserRepository.IsNicknameFree(userNickname)) { // interrupt command executing, wrong user's nickname has already been taken Core.Logger.GetLogger.LogAsync(Core.LogMode.Info, "Nickname can not be changed. It is occupied"); // show message userSingleViewModel.WindowManager.ShowMessageWindow(Core.Messages.Info.ViewModel.NICKNAME_IS_NOT_FREE); // interrupt command CommandState = Enums.Admin.CommandState.Interrupted; return; } // get new user's password string userPassword = user.Password; Core.Logger.GetLogger.LogAsync(Core.LogMode.Info, $"New user's password = {userPassword}"); if (userPassword.Length > Core.Configuration.DBConfig.PASSWORD_MAX_LENGTH || userPassword.Length < Core.Configuration.DBConfig.PASSWORD_MIN_LENGTH) { // interrupt command executing, wrong user's password length Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "User's password is wrong. Interrupt command executing"); // show message Services.WindowManager.Instance.ShowMessageWindow(Core.Messages.Info.Admin.ADMIN_WRONG_USER_PASSWORD_LENGTH); // interrupt command CommandState = Enums.Admin.CommandState.Interrupted; return; } // delete avatar from server if can if (string.IsNullOrEmpty(user.MainPhotoPath)) { // if avatar is null, and it has file on server // delete file from server if (System.IO.File.Exists(userSingleViewModel.RealAvatarPath)) { System.IO.File.Delete(userSingleViewModel.RealAvatarPath); } } // command has been successfully executed CommandState = Enums.Admin.CommandState.Executed; }