/// <summary> /// Add new tag /// </summary> /// <param name="expense">Expense saving model</param> /// <param name="userIdentity">Owner</param> /// <returns>Result message</returns> public Enums.Messages AddExpense(ExpenseModels.SaveExpenseModel expense, string userIdentity) { using (var context = new ExpenseDatabaseContext()) { //find tag. You can edit just yours tags var tag = context.Tags.FirstOrDefault(x => x.TagId == expense.Tag); var user = context.Users.FirstOrDefault(x => x.Login == userIdentity); // check user account if (user == null) { return Enums.Messages.UserNotRegistrate; } // new expense will add if (tag != null) { context.Expenses.Add(new Expense() { Amount = expense.Amount, DateOfMakingExpense = expense.DateOfMakingExpense.ToUniversalTime(), Description = expense.Description, Tag = tag, User = user }); return context.SaveChanges() >= 1 ? Enums.Messages.Good : Enums.Messages.ErorWriteInDatabase; } return Enums.Messages.ErorWriteInDatabase; } }
/// <summary> /// Add new tag /// </summary> /// <param name="tagName">Name of the tag</param> /// <param name="userIdentity">Owner</param> /// <returns>Result message</returns> public Enums.Messages AddTag(string tagName, string userIdentity) { using (var context = new ExpenseDatabaseContext()) { //find tag. You can edit just yours tags var tag = context.Tags.FirstOrDefault(x => x.Name == tagName && !x.IsDelete); var user = context.Users.FirstOrDefault(x => x.Login == userIdentity); if(user == null) { return Enums.Messages.UserNotRegistrate; } // new tag will add if (tag == null) { context.Tags.Add(new Tag() { Name = tagName, User = user }); return context.SaveChanges() >= 1 ? Enums.Messages.Good : Enums.Messages.ErorWriteInDatabase; } else { // record exist in user list if (tag.User == user || tag.User == null) { return Enums.Messages.Good; } //add new tag else { // exist in other userTagList context.Tags.Add(new Tag() { Name = tagName, User = user }); return context.SaveChanges() >= 1 ? Enums.Messages.Good : Enums.Messages.ErorWriteInDatabase; } } } }
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { using (var context = new ExpenseDatabaseContext()) { User user = context.Users.FirstOrDefault(u => u.Login == username); string passwordSHA = UserHelper.hashSHA512(password); if (user == null) { // ExpenseTrackingSystem.Database.Security.User User userRecord = new User(); userRecord.UserGuid = Guid.NewGuid().ToString(); userRecord.Login = username; userRecord.Password = passwordSHA; context.Users.Add(userRecord); try { if (context.SaveChanges() != 1) { status = MembershipCreateStatus.ProviderError; MvcApplication.log.Error(" Saving were not completed "); return null; } } catch (UpdateException ex) { status = MembershipCreateStatus.ProviderError; MvcApplication.log.ErrorException(" Exeption during save data in the database: " + ex.StackTrace, ex); return null; } } else { status = MembershipCreateStatus.DuplicateUserName; return null; } status = MembershipCreateStatus.Success; return null; } }
/// <summary> /// Delete expense /// </summary> /// <param name="id">id record</param> /// <param name="userIdentity">user account</param> /// <returns>result message</returns> public Enums.Messages DeleteExpense(int id, string userIdentity) { using (var context = new ExpenseDatabaseContext()) { //find expense. You can edit just yours expenses var expense = context.Expenses.FirstOrDefault(x => x.ExpenseId == id && x.User.Login == userIdentity); if (expense != null) { context.Expenses.Remove(expense); return context.SaveChanges() >= 1 ? Enums.Messages.Good : Enums.Messages.ErorWriteInDatabase; } return Enums.Messages.RecordNotFound; } }
/// <summary> /// Edit expense /// </summary> /// <param name="model">edit model</param> /// <param name="userIdentity">user account</param> /// <returns>result message</returns> public Enums.Messages EditExpense(ExpenseModels.DisplayExpenseModel model, string NewTag, string userIdentity) { using (var context = new ExpenseDatabaseContext()) { // find expense. You can edit just yours expenses var expense = context.Expenses.FirstOrDefault(x => x.ExpenseId == model.Id && x.User.Login == userIdentity); // seach new tag var tag = context.Tags.FirstOrDefault(x => !x.IsDelete && x.Name == NewTag); if (expense != null) { // if tag didn't find , than create new tag if (tag == null) { tag = (new Tag() { Name = NewTag, User = expense.User }); context.Tags.Add(tag); } // save data expense.Amount = model.Amount; expense.Tag = tag; expense.Description = model.Description; expense.DateOfMakingExpense = model.DateOfMakingExpense; return context.SaveChanges() >= 1 ? Enums.Messages.Good : Enums.Messages.ErorWriteInDatabase; } return Enums.Messages.RecordNotFound; } }
/// <summary> /// Delete tag /// </summary> /// <param name="id">Record id</param> /// <param name="userIdentity">User</param> /// <returns>Result message</returns> public Enums.Messages DeleteTag(int id, string userIdentity) { using (var context = new ExpenseDatabaseContext()) { //find tag. You can edit just yours tags. var tag = context.Tags.FirstOrDefault(x => !x.IsDelete && x.TagId == id); var user = context.Users.FirstOrDefault(x => x.Login == userIdentity); if (user == null) { return Enums.Messages.UserNotRegistrate; } if(tag != null && tag.User == user) { tag.IsDelete = true; return context.SaveChanges() >= 1 ? Enums.Messages.Good : Enums.Messages.ErorWriteInDatabase; } return Enums.Messages.RecordNotFound; } }
/// <summary> /// Edit tag /// </summary> /// <param name="id">id tag</param> /// <param name="tagName">New name</param> /// <returns>Result message</returns> public Enums.Messages EditTag(int id, string tagName, string userIdentity) { using (var context = new ExpenseDatabaseContext()) { //find tag. You can edit just yours tags var tag = context.Tags.FirstOrDefault(x => !x.IsDelete && x.TagId == id && x.User.Login == userIdentity); if (tag != null) { tag.Name = tagName; return context.SaveChanges() == 1 ? Enums.Messages.Good : Enums.Messages.ErorWriteInDatabase; } else { return Enums.Messages.RecordNotFound; } } }