/// <summary> /// Checks if user doesn't exist, if so, creates a new ApplicationUser /// </summary> public static async Task CheckCreateUser(DistilDBContext _db, Logger _logger, ApplicationUserManager UserManager, ApplicationUser user, string password, uint distillerId) { var existingUser = await UserManager.FindByNameAsync(user.UserName); if (existingUser == null) { var userCreationSucceeded = false; try { var result = await UserManager.CreateAsync(user, password); if (result.Succeeded) { userCreationSucceeded = true; } else { throw new Exception(string.Concat(result.Errors)); } } catch (Exception ex) { _logger.Error("Failed to create default admin user: {0}", ex.ToString()); } // Insert into AspNetUserToDistiller only after successfully creating a user if (userCreationSucceeded) { // Find newly created user var newUser = await UserManager.FindByNameAsync(user.UserName); try { _db.AspNetUserToDistiller.Add(new AspNetUserToDistiller { DistillerID = Convert.ToInt32(distillerId), UserId = newUser.Id }); _db.SaveChanges(); } catch (Exception ex) { // Something is wrong with the connection to the db, log and attempt to revert user creation _logger.Error("Failed to insert into AspNetUserToDistiller table: \n\t{0}", ex.ToString()); await UserManager.DeleteAsync(newUser); } } } }
/// <summary> /// CreateSpirit Method inserts new record in Spirit table /// </summary> /// <param name="spiritObject"></param> /// <returns>int</returns> public int CreateSpirit(int userId, SpiritObject spiritObject) { var distillerId = _dl.GetDistillerId(userId); //define method execution return value to be false by default var retMthdExecResult = 0; if (spiritObject != null) { try { Spirit tbl = new Spirit(); tbl.Name = spiritObject.SpiritName; tbl.ProcessingReportTypeID = spiritObject.ProcessingReportTypeID; tbl.DistillerID = distillerId; if (spiritObject.Note != string.Empty && spiritObject.Note != null) { tbl.Note = spiritObject.Note; } _db.Spirit.Add(tbl); _db.SaveChanges(); retMthdExecResult = tbl.SpiritID; } catch (Exception e) { retMthdExecResult = 0; } } else { retMthdExecResult = 0; } return(retMthdExecResult); }
private void CompleteDbTransaction() { _context.SaveChanges(); }
public bool UpdatePurchase(PurchaseObject purchaseObject, int userId) { bool retMthdExecResult = false; try { var purchT = (from rec in _db.Purchase join dslrs in _db.AspNetUserToDistiller on rec.DistillerID equals dslrs.DistillerID into dslrs_join from dslrs in dslrs_join.DefaultIfEmpty() where rec.PurchaseID == purchaseObject.PurchaseId && dslrs.UserId == userId select rec).FirstOrDefault(); if (purchT != null) { if (purchT.PurchaseName != purchaseObject.PurBatchName && purchaseObject.PurBatchName != null) { purchT.PurchaseName = purchaseObject.PurBatchName; } if (purchT.PurchaseDate != purchaseObject.PurchaseDate && purchaseObject.PurchaseDate != null) { purchT.PurchaseDateOffset = purchaseObject.PurchaseDate; purchT.PurchaseDate = purchaseObject.PurchaseDate.DateTime; } if (purchT.VendorID != purchaseObject.VendorId && purchaseObject?.VendorId != null) { purchT.VendorID = purchaseObject.VendorId; } if (purchT.Price != purchaseObject.Price && purchaseObject?.Price != null) { purchT.Price = purchaseObject.Price; } if (purchT.Note != purchaseObject.Note && purchaseObject.Note != null) { purchT.Note = purchaseObject.Note; } //todo: need to be able to add update for storages and Material Type(even though, updating material type might be difficult) _db.SaveChanges(); // Quantity if (purchT.VolumeID > 0 && purchaseObject.Quantity != null) { //update quantity record var qtyRec = (from rec in _db.Volume where rec.VolumeID == purchT.VolumeID select rec).FirstOrDefault(); if (qtyRec != null && qtyRec.Value != purchaseObject.Quantity) { qtyRec.Value = purchaseObject.Quantity; _db.SaveChanges(); } } else if (purchT.VolumeID == 0 && purchaseObject.Quantity != null) { //create quantity record Volume newQtyRec = new Volume(); newQtyRec.Value = purchaseObject.Quantity; _db.Volume.Add(newQtyRec); _db.SaveChanges(); purchT.VolumeID = newQtyRec.VolumeID; } if (purchaseObject.PurchaseType != "Supply" || purchaseObject.PurchaseType != "Additive") { // Volume By Weight if (purchT.WeightID != 0 && purchaseObject.VolumeByWeight != null) { //update volume by weight record var vbwRec = (from rec in _db.Weight where rec.WeightID == purchT.WeightID select rec).FirstOrDefault(); if (vbwRec != null && vbwRec.Value != purchaseObject.VolumeByWeight) { vbwRec.Value = purchaseObject.VolumeByWeight; _db.SaveChanges(); } } else if (purchT.WeightID == 0 && purchaseObject.VolumeByWeight != null) { //create volume by weight record Weight newVbwRec = new Weight(); newVbwRec.Value = purchaseObject.VolumeByWeight; _db.Weight.Add(newVbwRec); _db.SaveChanges(); purchT.WeightID = newVbwRec.WeightID; } } if (purchaseObject.PurchaseType == "Distilled" || purchaseObject.PurchaseType == "Fermented") { // Alcohol Content if (purchT.AlcoholID != 0 && purchaseObject.AlcoholContent != null) { //update alcohol content record var alcRec = (from rec in _db.Alcohol where rec.AlcoholID == purchT.AlcoholID select rec).FirstOrDefault(); if (alcRec != null && alcRec.Value != purchaseObject.AlcoholContent) { alcRec.Value = purchaseObject.AlcoholContent; _db.SaveChanges(); } } else if (purchT.AlcoholID == 0 && purchaseObject.AlcoholContent != null) { //create alcohol content record Alcohol newAlcRec = new Alcohol(); newAlcRec.Value = purchaseObject.AlcoholContent; _db.Alcohol.Add(newAlcRec); _db.SaveChanges(); purchT.AlcoholID = newAlcRec.AlcoholID; } // Proof if (purchT.ProofID != 0 && purchaseObject.ProofGallon != null) { //update proof record var prfRec = (from rec in _db.Proof where rec.ProofID == purchT.ProofID select rec).FirstOrDefault(); if (prfRec != null && prfRec.Value != purchaseObject.ProofGallon) { prfRec.Value = purchaseObject.ProofGallon; _db.SaveChanges(); } } else if (purchT.ProofID == 0 && purchaseObject.ProofGallon != null) { //create proof record Proof newPrfRec = new Proof(); newPrfRec.Value = purchaseObject.ProofGallon; _db.Proof.Add(newPrfRec); _db.SaveChanges(); purchT.ProofID = newPrfRec.ProofID; } // call Update Storage report table here } // update storages var storages = from rec in _db.StorageToRecord where rec.RecordId == purchT.PurchaseID && rec.TableIdentifier == "pur" select rec; // empty StorageToRecord table records first if (storages != null) { foreach (var i in storages) { _db.StorageToRecord.Remove(i); } _db.SaveChanges(); } if (purchaseObject.Storage != null) { string storagesString = string.Empty; // write new records to StorageToRecord table foreach (var k in purchaseObject.Storage) { StorageToRecord stoR = new StorageToRecord(); stoR.StorageID = k.StorageId; stoR.RecordId = purchT.PurchaseID; stoR.TableIdentifier = "pur"; _db.StorageToRecord.Add(stoR); _db.SaveChanges(); storagesString += k.StorageName + "; "; } purchaseObject.StorageName = storagesString; } } else { return(false); } retMthdExecResult = true; _dl.SavePurchaseHistory(purchaseObject, userId); } catch (Exception e) { throw; } return(retMthdExecResult); }