public IActionResult ValidateParticipants([FromBody] Dictionary <string, string> santas) { if (Request.Headers.Authorization != AdminSecret) { return(Unauthorized()); } var response = new ValidationDto(); using (var db = new SantaContext()) { foreach (var santa in santas) { response.Santas.Add(santa.Key); var thisSanta = db.Santas?.Find(Guid.Parse(santa.Value)); if (thisSanta == null) { return(UnprocessableEntity("EI KLAPI")); } var symmetricEncryptDecrypt = new EncryptionFactory(); var decryptedName = symmetricEncryptDecrypt.Decrypt(thisSanta.DesignatedPerson, thisSanta.IVBase64, EncryptionKey); response.Receivers.Add(decryptedName); } } response.Santas = response.Santas.OrderBy(x => x).ToList(); response.Receivers = response.Receivers.OrderBy(x => x).ToList(); return(Ok(response)); }
public UserController(UserManager <AppUser> userManager, RoleManager <AppRole> roleManager , SantaContext context) { _userManager = userManager; _roleManager = roleManager; _context = context; }
public IActionResult GetFamilies() { if (Request.Headers.Authorization != AdminSecret) { return(Unauthorized()); } using (var db = new SantaContext()) { var santas = db.Santas?.ToList() ?? new List <Santa>(); return(Ok(santas)); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, SantaContext context , UserManager <AppUser> userManager , RoleManager <AppRole> roleManager) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseAuthentication(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseCors("CorsPolicy"); app.UseMvc(); DummyData.Initialize(context, userManager, roleManager).Wait(); }
public IActionResult Get(Guid code) { using (var db = new SantaContext()) { Santa?santa = db.Santas?.First(x => x.Id == code); if (santa != null) { var symmetricEncryptDecrypt = new EncryptionFactory(); var decryptedName = symmetricEncryptDecrypt.Decrypt(santa.DesignatedPerson, santa.IVBase64, EncryptionKey); db.LogEntries?.Add(new LogEntry(santa.Id, Request.Headers["User-Agent"], Request.Headers["X-Forwarded-For"])); db.SaveChanges(); var response = new SantaDto { Name = santa.Name, DesignatedPerson = decryptedName }; return(Ok(response)); } else { return(BadRequest()); } } }
public IActionResult SetFamilies([FromBody] List <Family> families) { if (Request.Headers.Authorization != AdminSecret) { return(Unauthorized()); } var response = new Dictionary <string, string>(); bool done = false; using (var db = new SantaContext()) { db.Santas?.RemoveRange(db.Santas); db.SaveChanges(); do { var transaction = db.Database.BeginTransaction(); try { for (int i = 0; i < families.Count; i++) { Family currentFamily = families.ElementAt(i); IEnumerable <Family> otherFamilies = families.Where(x => x.Id != currentFamily.Id); foreach (Person person in currentFamily.Members) { //Assuming that this person is not gifting to anyone! //Get people that are not getting a gift from anyone, random them up List <Person> personsWithoutIsGiftedTo = otherFamilies .SelectMany(x => x.Members) .Where(x => !x.IsGiftedTo) .OrderBy(x => Guid.NewGuid()) .ToList(); Person giftingTo = personsWithoutIsGiftedTo.First(); person.GiftingTo = giftingTo; giftingTo.IsGiftedTo = true; } } done = true; foreach (Family family in families) { foreach (Person person in family.Members) { if (person.GiftingTo == null) { throw new Exception("GiftingTo is Null!"); } var name = person.GiftingTo.Name; var symmetricEncryptDecrypt = new EncryptionFactory(); var IVBase64 = symmetricEncryptDecrypt.InitSymmetricEncryptionIV(EncryptionKey); var encryptedName = symmetricEncryptDecrypt.Encrypt(name, IVBase64, EncryptionKey); var santa = new Santa(person.Name, encryptedName, IVBase64); db.Santas?.Add(santa); response.Add(person.Name, santa.Id.ToString()); } } db.LogEntries?.RemoveRange(db.LogEntries); db.SaveChanges(); } catch (Exception ex) { if (ex is InvalidOperationException) { //All good, this exception is expected, reset and try again! transaction.Rollback(); response = new Dictionary <string, string>(); } else { throw; } } transaction.Commit(); } while (!done); } return(Ok(response)); }