public async Task <IActionResult> InsereUsuario([FromBody] InsereUsuarioRequest insereUsuarioRequest)
        {
            try
            {
                if (insereUsuarioRequest?.Nome?.Length > 0)
                {
                    var usuario = new Usuario(insereUsuarioRequest.Nome);
                    await _context.AddAsync(usuario);

                    await _context.SaveChangesAsync();

                    return(Ok(ok));
                }

                return(BadRequest(usuarioNaoExiste));
            }
            catch
            {
                return(BadRequest(badRequest));
            }
        }
Exemplo n.º 2
0
 public async Task <T> AddAsync <T>(T entity) where T : class
 {
     try
     {
         await _context.AddAsync(entity);
     }
     catch (Exception error)
     {
         throw error;
     }
     return(entity);
 }
Exemplo n.º 3
0
        public async Task <ActionResult <SensorLog> > Log(string input)
        {
            // The post-request simulates a sensor-trigger and the paramater "input" consists of which section was entered and which section was exited, separated by ".".
            // Example: "Electronics.Kitchen" says a visitor entered the section "Electronics" and exited the section "Kitchen".
            // This was my way to handle the fact that if a visitor enters a section, they had to exit another.
            var enterName = input.Split(".")[0];
            var exitName  = input.Split(".")[1];

            var enterSection = await _context.StoreSections
                               .FirstOrDefaultAsync(ss => ss.Name == enterName);

            if (enterSection == default)
            {
                return(BadRequest(new { Error = $"Section '{enterSection}' does not exists" }));
            }
            var exitSection = await _context.StoreSections
                              .FirstOrDefaultAsync(ss => ss.Name == exitName);

            if (exitSection == default)
            {
                return(BadRequest(new { Error = $"Section '{exitSection}' does not exists" }));
            }

            // TODO: The visitor count can be less than zero.

            enterSection.VisitorCount++;
            exitSection.VisitorCount--;

            var log = new SensorLog()
            {
                TimeStamp         = DateTime.Now,
                EnterStoreSection = enterSection,
                ExitStoreSection  = exitSection,
                Direction         = Direction.Enter
            };
            await _context.AddAsync(log);

            await _context.SaveChangesAsync();

            string warning = "";

            if (exitSection.VisitorCount < 0)
            {
                warning = $"Warning, section '{exitSection.Name}' has less then 0 visitors. Something is wrong with the sensors, not the code ;)";
            }

            return(Created(Request.HttpContext.Request.Path, new
            {
                Log = $"Enter : {enterSection.Name}, Exit : {exitSection.Name}, Time Stamp : {DateTime.Now}",
                Warning = String.IsNullOrEmpty(warning) ? "No warnings, all good" : warning
            }));
        }
Exemplo n.º 4
0
            private async Task <Person> GetPerson(Command request)
            {
                if (!request.Id.HasValue)
                {
                    var person = new Person();

                    await _apiContext.AddAsync(person);

                    return(person);
                }

                return(await _apiContext
                       .Set <Person>()
                       .FirstOrDefaultAsync(e => e.Id == request.Id));
            }
        public async Task <Customer> Insert(Customer customer)
        {
            try
            {
                await _context.AddAsync(customer);

                await _context.SaveChangesAsync();

                return(customer);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Register([FromBody] UserModel user)
        {
            if (user == null || user.Password == null || user.Email == null)
            {
                return(BadRequest("Некоректні дані"));
            }

            User u = await _context.Users.FirstOrDefaultAsync(u => u.Email == user.Email);

            if (u == null)
            {
                await _context.AddAsync(new User
                {
                    Email    = user.Email,
                    Username = user.Username,
                    Password = user.Password,
                    Folders  = new List <Folder>
                    {
                        new Folder()
                        {
                            Name = "Today", Color = "white", Required = true
                        },
                        new Folder()
                        {
                            Name = "Tasks", Color = "white", Required = true
                        }
                    }
                });

                await _context.SaveChangesAsync();

                var userAuth = await _loginService.AuthenticateUserAsync(user);

                var tokenString = _loginService.GenerateJSONWebToken(user);

                return(Ok(new { token = tokenString, expires = Convert.ToInt32(_config["Jwt:ExpiresMinutes"]) }));
            }
            else
            {
                return(BadRequest("Даний користувач уже існує"));
            }
        }
Exemplo n.º 7
0
        public async Task <int?> StoreDonations(string name, string postcode, int donationAmount)
        {
            var donation = new Donations
            {
                Name           = name,
                Postcode       = postcode,
                DonationAmount = donationAmount
            };

            var resultAdd = await _apiContext.AddAsync(donation);

            if (resultAdd.State == Microsoft.EntityFrameworkCore.EntityState.Added)
            {
                var resultSave = await _apiContext.SaveChangesAsync();

                return(resultSave == 1 ? resultAdd.Entity.Id : (int?)null);
            }

            return(null);
        }
Exemplo n.º 8
0
 public async Task AddAsync(T item)
 {
     await ApiContext.AddAsync(item);
 }
        public async Task <IActionResult> SaveWeatherAsync([FromBody] WeatherForecast model)
        {
            await apiContext.AddAsync(model);

            return(new JsonResult(new { ResponseMessage = "success" }));
        }
Exemplo n.º 10
0
        public static async Task Initialize(UserManager <User> userManager, ApiContext context, RoleManager <IdentityRole> roleManager)
        {
            string adminEmail = "*****@*****.**";
            string password   = "******";

            if (await userManager.FindByNameAsync(adminEmail) == null)
            {
                await roleManager.CreateAsync(new IdentityRole("admin"));

                await roleManager.CreateAsync(new IdentityRole("lead"));

                await roleManager.CreateAsync(new IdentityRole("client"));

                User admin = new User {
                    Email = adminEmail, UserName = adminEmail
                };
                await userManager.CreateAsync(admin, password);

                await userManager.AddToRoleAsync(admin, "admin");

                User lead = new User {
                    Email = "*****@*****.**", UserName = "******", FirstName = "Dustin", LastName = "Carroll"
                };
                await userManager.CreateAsync(lead, password);

                await userManager.AddToRoleAsync(lead, "lead");

                User client = new User {
                    Email = "*****@*****.**", UserName = "******", FirstName = "John", LastName = "Hale"
                };
                await userManager.CreateAsync(client, password);

                await userManager.AddToRoleAsync(client, "client");

                User admin1 = new User {
                    Email = "*****@*****.**", UserName = "******", FirstName = "Douglas", LastName = "Henry"
                };
                await userManager.CreateAsync(admin1, password);

                await userManager.AddToRoleAsync(admin1, "admin");

                User client1 = new User {
                    Email = "*****@*****.**", UserName = "******", FirstName = "Dale", LastName = "Hopkins"
                };
                await userManager.CreateAsync(client1, password);

                await userManager.AddToRoleAsync(client1, "client");

                Region Africa = new Region {
                    Name = "Africa"
                };
                Region Antarctica = new Region {
                    Name = "Antarctica"
                };
                Region AsiaPacific = new Region {
                    Name = "Asia/Pacifig"
                };
                Region Europe = new Region {
                    Name = "Europe"
                };
                Region NorthAmerica = new Region {
                    Name = "North America"
                };
                Region SouthAmerica = new Region {
                    Name = "South America"
                };
                await context.Regions.AddRangeAsync(Africa, Antarctica, AsiaPacific, Europe, NorthAmerica, SouthAmerica);

                Country Australia = new Country {
                    Capital = "Canberra", Name = "Australia", Region = AsiaPacific
                };
                Country Germany = new Country {
                    Capital = "Berlin", Name = "Germany", Region = Europe
                };
                Country Slovakia = new Country {
                    Capital = "Bratislava", Name = "Slovakia", Region = Europe
                };
                Country USA = new Country {
                    Capital = "Washington", Name = "Unated States", Region = NorthAmerica
                };
                Country TM = new Country {
                    Capital = "Ashgabat", Name = "Turkmenistan", Region = AsiaPacific
                };
                await context.Countries.AddRangeAsync(Australia, Germany, Slovakia, USA);

                CompanyQualification Qualified = new CompanyQualification {
                    QualificationName = "Qualified"
                };
                CompanyQualification NotQualified = new CompanyQualification {
                    QualificationName = "NotQualified"
                };
                CompanyQualification NewCompany = new CompanyQualification {
                    QualificationName = "NewCompany"
                };
                await context.CompanyQualifications.AddRangeAsync(Qualified, NotQualified, NewCompany);

                Linkedin SlastininLinkedin = new Linkedin {
                    FullLink = "linkedin.com/in/aleksandr-slastinin-379654183/"
                };
                Linkedin TTRLinkedin = new Linkedin {
                    FullLink = "linkedin.com/company/turkmen-tranzit/"
                };
                Linkedin TTWLinkedin = new Linkedin {
                    FullLink = "linkedin.com/company/ttweb/"
                };
                await context.Linkedins.AddRangeAsync(SlastininLinkedin, TTRLinkedin, TTWLinkedin);

                await context.SaveChangesAsync();

                Contact contact1 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact2 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact3 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact4 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact5 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact6 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact7 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact8 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact9 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact10 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact11 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact contact12 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact TtrContact1 = new Contact
                {
                    Email     = "*****@*****.**",
                    FirstName = "Aleksandr",
                    Position  = "department head",
                    Surname   = "Slastinin",
                    Linkedin  = SlastininLinkedin
                };
                Contact TtrContact2 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact TtrContact3 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact TtwebContact1 = new Contact
                {
                    Email     = "*****@*****.**",
                    FirstName = "Irada",
                    Position  = "Project Manager",
                    Surname   = "davletowa",
                };
                Contact TtwebContact2 = new Contact {
                    Email = "*****@*****.**"
                };
                Contact TtwebContact3 = new Contact {
                    Email = "*****@*****.**"
                };

                await context.Contacts.AddRangeAsync(contact1, contact2, contact3, contact4, contact5, contact6, contact7, contact8, contact9, contact10, contact11, contact12, TtrContact1, TtrContact2, TtrContact3, TtwebContact1, TtwebContact2, TtwebContact3);

                await context.SaveChangesAsync();

                Company ACN = new Company
                {
                    CompanyLegalName = "ATM ATM Pty Ltd",
                    HGBasedInCountry = Australia,
                    Qualification    = Qualified,
                    TradingName      = "A.C.N.",
                    Website          = "b24.turkmen-tranzit.com"
                };
                await context.Companies.AddAsync(ACN);

                CompanyContactLink ContactACN1 = new CompanyContactLink
                {
                    Company = ACN,
                    Contact = contact1
                };
                CompanyContactLink ContactACN2 = new CompanyContactLink
                {
                    Company = ACN,
                    Contact = contact2
                };
                CompanyContactLink ContactACN3 = new CompanyContactLink
                {
                    Company = ACN,
                    Contact = contact3
                };
                await context.CompanyContactLinks.AddRangeAsync(ContactACN1, ContactACN2, ContactACN3);

                Company ATM = new Company
                {
                    CompanyLegalName = "A.C.N. 605 479 678 Pty Ltd",
                    HGBasedInCountry = Australia,
                    Qualification    = Qualified,
                    TradingName      = "ATM",
                    LeadOwner        = lead,
                    QualifiedDate    = DateTime.Now,
                    Website          = "ttweb.org/blog"
                };
                await context.Companies.AddAsync(ATM);

                CompanyContactLink ContactATM1 = new CompanyContactLink
                {
                    Company = ATM,
                    Contact = contact4
                };
                CompanyContactLink ContactATM2 = new CompanyContactLink
                {
                    Company = ATM,
                    Contact = contact5
                };
                CompanyContactLink ContactATM3 = new CompanyContactLink
                {
                    Company = ATM,
                    Contact = contact6
                };
                await context.CompanyContactLinks.AddRangeAsync(ContactATM1, ContactATM2, ContactATM3);

                Company Pay = new Company
                {
                    CompanyLegalName = "24-pay s.r.o",
                    HGBasedInCountry = Australia,
                    Qualification    = NotQualified,
                    TradingName      = "24 Pay SRO",
                    LeadOwner        = lead,
                    Website          = "24-pay.sk"
                };
                await context.Companies.AddAsync(Pay);

                CompanyContactLink ContactPay1 = new CompanyContactLink
                {
                    Company = Pay,
                    Contact = contact7
                };
                CompanyContactLink ContactPay2 = new CompanyContactLink
                {
                    Company = Pay,
                    Contact = contact8
                };
                CompanyContactLink ContactPay3 = new CompanyContactLink
                {
                    Company = Pay,
                    Contact = contact9
                };
                await context.CompanyContactLinks.AddRangeAsync(ContactPay1, ContactPay2, ContactPay3);

                Company Ttz = new Company
                {
                    CompanyLegalName = "HO ''Turkmen-Tranzit''",
                    HGBasedInCountry = TM,
                    Qualification    = NewCompany,
                    TradingName      = "Turkmen-Tranzit",
                    Website          = "Turkmen-Tranzit.com",
                    CompanyLinkedin  = TTRLinkedin
                };
                await context.Companies.AddAsync(Ttz);

                CompanyContactLink ContactTtz1 = new CompanyContactLink
                {
                    Company = Ttz,
                    Contact = TtrContact1
                };
                CompanyContactLink ContactTtz2 = new CompanyContactLink
                {
                    Company = Ttz,
                    Contact = TtrContact2
                };
                CompanyContactLink ContactTtz3 = new CompanyContactLink
                {
                    Company = Ttz,
                    Contact = TtrContact3
                };
                await context.CompanyContactLinks.AddRangeAsync(ContactTtz1, ContactTtz2, ContactTtz3);

                await context.SaveChangesAsync();

                Company Ttw = new Company
                {
                    CompanyLegalName = "HO ''Turkmen-Tranzit'' -> TTWeb",
                    HGBasedInCountry = TM,
                    Qualification    = Qualified,
                    TradingName      = "TtWeb",
                    Website          = "Ttweb.org",
                    CompanyLinkedin  = TTWLinkedin
                };
                await context.Companies.AddAsync(Ttw);

                CompanyContactLink ContactTtw1 = new CompanyContactLink
                {
                    Company = Ttw,
                    Contact = TtwebContact1
                };
                CompanyContactLink ContactTtw2 = new CompanyContactLink
                {
                    Company = Ttw,
                    Contact = TtwebContact2
                };
                CompanyContactLink ContactTtw3 = new CompanyContactLink
                {
                    Company = Ttw,
                    Contact = TtwebContact3
                };
                await context.CompanyContactLinks.AddRangeAsync(ContactTtw1, ContactTtw2, ContactTtw3);

                await context.SaveChangesAsync();

                Company google = new Company
                {
                    CompanyLegalName = "google",
                    HGBasedInCountry = USA,
                    Qualification    = Qualified,
                    TradingName      = "google",
                    Website          = "google.com"
                };
                await context.Companies.AddAsync(google);

                CompanyContactLink ContactGoogle1 = new CompanyContactLink
                {
                    Company = google,
                    Contact = contact10
                };
                CompanyContactLink ContactGoogle2 = new CompanyContactLink
                {
                    Company = google,
                    Contact = contact11
                };
                CompanyContactLink ContactGoogle3 = new CompanyContactLink
                {
                    Company = google,
                    Contact = contact12
                };
                await context.CompanyContactLinks.AddRangeAsync(ContactGoogle1, ContactGoogle2, ContactGoogle3);

                Company microsoft = new Company
                {
                    CompanyLegalName = "microsoft",
                    HGBasedInCountry = USA,
                    Qualification    = Qualified,
                    TradingName      = "microsoft",
                    Website          = "microsoft.com"
                };
                await context.Companies.AddAsync(microsoft);

                AdvertisingCompany advertisingCompany = new AdvertisingCompany
                {
                    Id   = "cam_r7jfwbKo46XiS9okW",
                    Name = "ttweb"
                };

                await context.AddAsync(advertisingCompany);


                await context.SaveChangesAsync();
            }
        }