public IHttpActionResult Post([FromBody] BankUser value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { using (BankContext db = new BankContext()) { string userId = RequestContext.Principal.Identity.GetUserId(); value.UserIdentityId = userId; if (!db.BankUsers.Any(u => u.UserIdentityId == userId)) { db.BankUsers.Add(value); db.SaveChanges(); var createdUser = db.BankUsers.FirstOrDefault(u => u.UserIdentityId == userId); //send hellomail return(Ok(createdUser)); } } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(BadRequest()); }
public IList <PayInfo> GetByCard(int cardId) { IList <PayInfo> userPays = new List <PayInfo>(); try { using (BankContext db = new BankContext()) { var userPaysEnum = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u => u.UserIdentityId == UserId)?.Pays.Where(p => p.CardId == cardId); if (!userPaysEnum.Any()) { CreateFirstData(db, cardId); userPaysEnum = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u => u.UserIdentityId == UserId)?.Pays.Where(p => p.CardId == cardId); } return(userPaysEnum.ToList()); } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(userPays); }
public IList <PayInfo> Get() { IList <PayInfo> userPays = new List <PayInfo>(); try { using (BankContext db = new BankContext()) { var userCardIDs = db.BankUsers.Include(u => u.Cards).FirstOrDefault(u => u.UserIdentityId == UserId).Cards.Where(c => c.IsActive).Select(c => c.Id).ToArray(); foreach (var userCardID in userCardIDs) { var cardPays = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u => u.UserIdentityId == UserId)?.Pays.Where(p => p.CardId == userCardID); if (!cardPays.Any()) { CreateFirstData(db, userCardID); } } userPays = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u => u.UserIdentityId == UserId)?.Pays.Where(p => userCardIDs.Contains(p.CardId)).ToList(); } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(userPays); }
public IHttpActionResult Put(int id, [FromBody] CardInfo value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var controllerValidate = ValidateCard(value); if (controllerValidate.Count > 0) { foreach (var errorText in controllerValidate) { ModelState.AddModelError("", errorText); } return(BadRequest(ModelState)); } CardInfo retCard = null; try { using (BankContext db = new BankContext()) { var currentUser = db.BankUsers.Include(c => c.Cards).FirstOrDefault(u => u.UserIdentityId == UserId && u.Cards.Any(c => c.Id == id)); if (currentUser != null) { var cardForModify = currentUser.Cards.FirstOrDefault(c => c.Id == id); if (cardForModify != null) { foreach (PropertyInfo propertyInfo in cardForModify.GetType().GetProperties() .Where(p => p.Name != nameof(CardInfo.Id) && p.Name != nameof(CardInfo.Balance))) { if (propertyInfo.GetValue(value, null) != null) { propertyInfo.SetValue(cardForModify, propertyInfo.GetValue(value, null), null); } } db.SaveChanges(); } retCard = currentUser.Cards.FirstOrDefault(c => c.Id == id); } } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } if (retCard != null) { return(Ok(retCard)); } else { return(BadRequest()); } }
public IHttpActionResult Post([FromBody] CardInfo value) { CardInfo retCard = null; try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var controllerValidate = ValidateCard(value); if (controllerValidate.Count > 0) { foreach (var errorText in controllerValidate) { ModelState.AddModelError("", errorText); } return(BadRequest(ModelState)); } using (BankContext db = new BankContext()) { var currentUser = db.BankUsers.Include(u => u.Cards).FirstOrDefault(u => u.UserIdentityId == UserId); if (currentUser != null) { currentUser.Cards.Add(value); db.SaveChanges(); retCard = currentUser.Cards.FirstOrDefault(c => c.Id == value.Id); } } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } if (retCard != null) { return(Ok(retCard)); } else { return(BadRequest()); } }
//TODO test protected void AttachDBInternal(DbConnection connection, ExternalDatastore externalDB) { if (connection != null) { var commandText = "ATTACH DATABASE \"" + externalDB.DbPath + "\" AS " + externalDB.Alias + ";"; try { connection.ExecuteNonQuery(commandText, (object[])null, CurrentTransaction); } catch (Exception e) { throw ExceptionProcessor.ProcessException(e, connection, commandText, CurrentTransaction); } } }
public BankUser Get() { try { using (BankContext db = new BankContext()) { string userId = RequestContext.Principal.Identity.GetUserId(); var findedUser = db.BankUsers.FirstOrDefault(u => u.UserIdentityId == userId); return(findedUser); } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(null); }
public IHttpActionResult Delete(int id) { try { using (BankContext db = new BankContext()) { var userForDelete = db.BankUsers.FirstOrDefault(u => u.Id == id); db.BankUsers.Remove(userForDelete); db.SaveChanges(); } return(Ok()); } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(InternalServerError()); }
//TODO test protected void DetachDBInternal(ExternalDatastore externalDB) { lock (PersistentConnectionSyncLock) { var connection = PersistentConnection; if (connection != null) { var commandText = "DETACH DATABASE \"" + externalDB.Alias + "\";"; try { connection.ExecuteNonQuery(commandText, (object[])null, CurrentTransaction); } catch (Exception e) { throw ExceptionProcessor.ProcessException(e, connection, commandText, CurrentTransaction); } } } }
public CardInfo Get(int id) { CardInfo retCard = null; try { using (BankContext db = new BankContext()) { retCard = db.BankUsers .Include(u => u.Cards) .FirstOrDefault(u => u.UserIdentityId == RequestContext.Principal.Identity.GetUserId())?.Cards .FirstOrDefault(c => c.Id == id && c.IsActive); } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(retCard); }
public PayInfo Get(int id) { PayInfo userPay = null; try { using (BankContext db = new BankContext()) { userPay = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u => u.UserIdentityId == UserId)?.Pays.FirstOrDefault(p => p.Id == id); } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(userPay); }
public void Delete(int id) { try { using (BankContext db = new BankContext()) { var currentUser = db.BankUsers.Include(c => c.Cards).FirstOrDefault(u => u.UserIdentityId == UserId && u.Cards.Any(c => c.Id == id)); if (currentUser != null) { //currentUser.Cards.Remove(currentUser.Cards.FirstOrDefault(c => c.Id == id)); currentUser.Cards.FirstOrDefault(c => c.Id == id).IsActive = false; db.SaveChanges(); } } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } }
public IEnumerable <CardInfo> Get() { IList <CardInfo> userCards = new List <CardInfo>(); try { using (BankContext db = new BankContext()) { userCards = db.BankUsers .Include(u => u.Cards) .FirstOrDefault(u => u.UserIdentityId == UserId) ?.Cards.Where(c => c.IsActive) .ToList(); } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(userCards); }
private List <string> ValidateCard(CardInfo cardInfo) { var results = new List <string>(); try { using (BankContext db = new BankContext()) { if (db.BankUsers .Include(u => u.Cards) .Any(u => u.UserIdentityId != UserId && u.Cards.Any(c => c.CardNumber == cardInfo.CardNumber))) { results.Add("This card is not yours"); } } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(results); }
public IList <BankBranch> Get() { IList <BankBranch> retValues = new List <BankBranch>(); try { using (BankContext db = new BankContext()) { retValues = db.BankBranches.ToList(); if (!retValues.Any()) { CreateFirstData(db); retValues = db.BankBranches.ToList(); } } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(retValues); }
public IHttpActionResult Put(int id, [FromBody] BankUser value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { using (BankContext db = new BankContext()) { string userId = RequestContext.Principal.Identity.GetUserId(); var userForModify = db.BankUsers.FirstOrDefault(u => u.Id == id && u.UserIdentityId == userId); if (userForModify != null) { foreach (PropertyInfo propertyInfo in userForModify.GetType().GetProperties() .Where(p => p.Name != nameof(BankUser.Id))) { if (propertyInfo.GetValue(value, null) == null) { propertyInfo.SetValue(value, propertyInfo.GetValue(userForModify, null), null); } } db.Entry(userForModify).CurrentValues.SetValues(value); db.Entry(userForModify).State = EntityState.Modified; db.SaveChanges(); var createdUser = db.BankUsers.FirstOrDefault(u => u.UserIdentityId == userId); return(Ok(createdUser)); } } } catch (Exception e) { ExceptionProcessor.ProcessException(e); } return(BadRequest()); }