public Operation <List <ServiceModel> > UploadServiceNames(Stream stream, ServiceModel model) { return(Operation.Create((Func <List <ServiceModel> >)(() => { var sheet = _excel.Load <ServiceModel>(stream); var errors = new List <ServiceModel>(); foreach (var row in sheet) { var servNm = _db.Query <Service>().Where(v => v.ServId == row.ServId).FirstOrDefault(); // var votername = _votgr.GetVoters().Result.Where(v => v.StaffName == model.StaffName).FirstOrDefault(); if (servNm == null) { row.Message = "service does not exist"; errors.Add(row); continue; } else { row.Message = "service does not exist"; } row.CreatedBy = model.CreatedBy; row.ModifiedBy = model.ModifiedBy; row.ServerDescription = model.ServerDescription; row.Services = model.Services; row.Email = model.Email; row.ExpiringDate = row.ExpiringDate /*== staffNm.StaffName ? row.StaffName : staffNm.StaffName*/; row.RenewerType = row.RenewerType; row.CountDown = row.CountDown; row.Access_Details = row.Access_Details /*== staffNm.StaffNo ? row.StaffNo : staffNm.StaffNo*/; if (servNm == null) { var entity = row.Create(row); _db.Add(entity); } else { var entity = row.Edit(servNm, row); _db.Update(entity); } var result = _db.SaveChanges(); if (result.Succeeded == false) { row.Message = result.Message; errors.Add(row); } } return errors; }))); }
public Operation <List <VoterModel> > UploadVoterNames(Stream stream, VoterModel model) { return(Operation.Create(() => { var sheet = _excel.Load <VoterModel>(stream); var errors = new List <VoterModel>(); foreach (var row in sheet) { // note: I check if staffNo exist in the database, if null, add the data and save it. if yes, edit the data and save it. var voter = _context.Voters.Where(v => v.StaffName == row.StaffName && v.StaffNo == row.StaffNo && v.Email == row.Email && v.Department == row.Department).FirstOrDefault(); row.CreatedBy = model.CreatedBy; row.ModifiedBy = model.ModifiedBy; row.CreatedDate = DateTime.Now; if (voter != null) { throw new Exception("Name already exist"); } //{ if (row.StaffName == null || row.StaffNo == null || row.Email == null) { throw new Exception("An Empty cell in the file"); } var voterEntity = new Voter { CreatedBy = row.CreatedBy, ModifiedBy = row.ModifiedBy, CreatedDate = DateTime.Now, StaffName = row.StaffName, Department = row.Department, StaffNo = row.StaffNo, Email = row.Email //ModifiedDate = DateTime.Now }; _context.Voters.Add(voterEntity); //continue; //} //else //{ // voter.StaffNo = row.StaffNo; // voter.StaffName = row.StaffName; // voter.Email = row.Email; // voter.CreatedDate = DateTime.Now; // //var entity1 = row.Edit(voter, row); // _context.Entry(voter); //} //errors.Add(row); } _context.SaveChanges(); return errors; })); }
public Operation <List <VoterModel> > UploadVoterNames(Stream stream, VoterModel model) { return(Operation.Create(() => { var sheet = _excel.Load <VoterModel>(stream); var errors = new List <VoterModel>(); foreach (var row in sheet) { // note: I check if staffNo exist in the database, if null, add the data and save it. if yes, edit the data and save it. var voter = _context.Voters.Where(v => v.StaffNo == row.StaffNo).FirstOrDefault(); row.CreatedBy = model.CreatedBy; row.ModifiedBy = model.ModifiedBy; row.CreatedDate = DateTime.Now; if (voter == null) { var voterEntity = new Voter { CreatedBy = row.CreatedBy, ModifiedBy = row.ModifiedBy, CreatedDate = DateTime.Now, StaffName = row.StaffName, StaffNo = row.StaffNo, Email = row.Email //ModifiedDate = DateTime.Now }; _context.Voters.Add(voterEntity); //var appuser = new ApplicationUser() { // UserName = row.StaffName, // PasswordHash = row.StaffNo, // Email = row.Email //}; //_usermanager.CreateAsync(appuser, appuser.PasswordHash); continue; } else { voter.StaffNo = row.StaffNo; voter.StaffName = row.StaffName; voter.Email = row.Email; voter.CreatedDate = DateTime.Now; //var entity1 = row.Edit(voter, row); _context.Entry(voter); } errors.Add(row); } _context.SaveChanges(); return errors; })); }
public Operation <List <VoterModel> > UploadStaffNames(Stream stream, VoterModel model) { return(Operation.Create((Func <List <VoterModel> >)(() => { var sheet = _excel.Load <VoterModel>(stream); var errors = new List <VoterModel>(); foreach (var row in sheet) { var staffNm = _db.Query <Voter>().Where(v => v.VoterId == row.VoterId).FirstOrDefault(); // var votername = _votgr.GetVoters().Result.Where(v => v.StaffName == model.StaffName).FirstOrDefault(); if (staffNm == null) { row.Message = "Voter does not exist"; errors.Add(row); continue; } else { row.Message = "Voter does not exist"; } row.CreatedBy = model.CreatedBy; row.ModifiedBy = model.ModifiedBy; row.StaffName = model.StaffName; row.Department = row.Department; row.StaffNo = model.StaffNo; row.Email = model.Email; row.StaffName = row.StaffName /*== staffNm.StaffName ? row.StaffName : staffNm.StaffName*/; row.Department = row.Department; row.StaffNo = row.StaffNo; row.Email = row.Email /*== staffNm.StaffNo ? row.StaffNo : staffNm.StaffNo*/; if (staffNm == null) { var entity = row.Create(row); _db.Add(entity); } else { var entity = row.Edit(staffNm, row); _db.Update(entity); } var result = _db.SaveChanges(); if (result.Succeeded == false) { row.Message = result.Message; errors.Add(row); } } return errors; }))); }
public Operation <List <ServerModel> > UploadServerNames(Stream stream, ServerModel model) { return(Operation.Create(() => { var sheet = _excel.Load <ServerModel>(stream); var errors = new List <ServerModel>(); foreach (var row in sheet) { // note: I check if staffNo exist in the database, if null, add the data and save it. if yes, edit the data and save it. var service = _context.Servers.Where(v => v.Discription == row.Discription && v.Access_Details == row.Access_Details && v.Charge == row.Charge && v.HardDisk == row.HardDisk && v.HDD_Available == row.HDD_Available && v.HDD_Used == row.HDD_Used && v.QTY == row.QTY && v.RAM == row.RAM && v.Services == row.Services && v.Total == row.Total).FirstOrDefault(); row.CreatedBy = model.CreatedBy; row.ModifiedBy = model.ModifiedBy; row.CreatedDate = DateTime.Now; ////if (service != null) throw new Exception("Name already exist"); //////{ //if (row.Discription == null || row.Access_Details == null || row.Charge == null || row.HardDisk == null || row.HDD_Available == null || row.HDD_Used == null || row.QTY == null || row.RAM == null || row.Services == null || row.Total == null) //{ // throw new Exception("An Empty cell in the file"); //} string date = row.ExpiringDate; var voterEntity = new Server { CreatedBy = row.CreatedBy, ModifiedBy = row.ModifiedBy, CreatedDate = DateTime.Now, Discription = row.Discription, ExpiringDate = DateTime.ParseExact(date, "dd/MM/yyyy", null), Access_Details = row.Access_Details, Charge = row.Charge, HardDisk = row.HardDisk, HDD_Available = row.HDD_Available, HDD_Used = row.HDD_Used, QTY = row.QTY, Total = row.Total, Services = row.Services, RAM = row.RAM }; _context.Servers.Add(voterEntity); } _context.SaveChanges(); return errors; })); }
public Operation <List <ServiceModel> > UploadServiceNames(Stream stream, ServiceModel model) { return(Operation.Create(() => { var sheet = _excel.Load <ServiceModel>(stream); var errors = new List <ServiceModel>(); foreach (var row in sheet) { // note: I check if staffNo exist in the database, if null, add the data and save it. if yes, edit the data and save it. var service = _context.Services.Where(v => v.ServerDescription == row.ServerDescription && v.Services == row.Services && v.Access_Details == row.Access_Details && v.RenewerType == row.RenewerType && v.Email == row.Email && v.CountDown == row.CountDown && v.AlertExpired == row.AlertExpired).FirstOrDefault(); row.CreatedBy = model.CreatedBy; row.ModifiedBy = model.ModifiedBy; row.CreatedDate = DateTime.Now; ////if (service != null) throw new Exception("Name already exist"); //////{ //if (row.ServerDescription == null || row.Services == null || row.Access_Details == null || row.ExpiringDate == null /*|| row.RenewerType == null || row.Email == null || row.CountDown == null || row.AlertExpired == null*/) //{ // throw new Exception("An Empty cell in the file"); //} string date = row.ExpiringDate; var voterEntity = new Service { CreatedBy = row.CreatedBy, ModifiedBy = row.ModifiedBy, CreatedDate = DateTime.Now, ServerDescription = row.ServerDescription, Access_Details = row.Access_Details, Services = row.Services, ExpiringDate = date, RenewerType = row.RenewerType, Email = row.Email, CountDown = row.CountDown, AlertExpired = row.AlertExpired, //ModifiedDate = DateTime.Now }; _context.Services.Add(voterEntity); } _context.SaveChanges(); return errors; })); }