/// <summary> /// Method to Save User. Once the User is added in the database /// it will be added in the Users observable collection and Property Changed will be raised /// </summary> /// <param name="user"></param> void SaveUser(User user) { Color bgcolor = Color.Black; Color fcolor = Color.WhiteSmoke; Image image = ConvertTextToImage(user.FirstName.Substring(0, 1) + user.LastName.Substring(0,1), "Times New Roman", 25, bgcolor, fcolor, 50, 50); user.UserPhoto = ImageToByteArray(image); _dataService.CreateUser( (UserId, error) => { if (error != null) { // Report error here Console.WriteLine("The following exception has been raised: " + error); return; } if (UserId != 0) { //Close the current focused window var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive); window.Close(); //Send a message to Main ViewModel for refreshing the People on the MainWindow. Messenger.Default.Send(new NotificationMessage("RefreshUsers"), MessengerToken.RefreshUsers); } }, user); }
//Method to add user public void CreateUser(Action<int, Exception> callback, User user) { try { //Check if a tuple in the table is present in the database, and add it, if it's not available. if (ctx.Entry(user).State == System.Data.Entity.EntityState.Detached) { ctx.Users.Add(user); } ctx.SaveChanges(); callback(user.UserId, null); } catch (Exception e) { callback(0, e); } }
/// <summary> /// The method to send a message to add a new user /// to the UserViewModel /// </summary> public void SendUser() { User user = new User(); //Send User as a message to UserViewModel for opening a new window. Messenger.Default.Send<MessageCommunicator>(new MessageCommunicator() { User = user }, MessengerToken.AddUser); }