Beispiel #1
0
        public async Task <ActionResult> CriaBeaconAsync(CityList model)
        {
            string      men         = model.CreateBeacon.Mesage;
            string      username    = User.Identity.GetUserName();
            DataService dataService = new DataService();
            Admin       ad          = new Admin();

            ad = await dataService.GetAdminByIdAsync(username);

            string admin = "";
            int    flag  = 0;

            if (ad == null)
            {
                SuperAdmin superadmin = new SuperAdmin();
                superadmin = await dataService.GetSuperAdminByIdAsync(username);

                admin = superadmin.email;
                flag  = 1;
            }
            else
            {
                admin = ad.email;
                flag  = 0;
            }

            Beacon beacon = new Beacon();

            beacon.beaconID   = model.CreateBeacon.BeaconId;
            beacon.majorvalue = model.CreateBeacon.MajorValue;
            beacon.minorvalue = model.CreateBeacon.MinorValue;
            beacon.model      = model.CreateBeacon.Model;
            if (model.CreateBeacon.Nome.Contains("-") || model.CreateBeacon.Mesage.Contains("-"))
            {
                ModelState.AddModelError("", "Beacon should not contain - in the name and in the message.");
                return(View(model));
            }
            if (model.CreateBeacon.ActiveBeacon == true)
            {
                beacon.name = "active-" + model.CreateBeacon.Nome + "-0";
            }
            else
            {
                beacon.name = "inactive-" + model.CreateBeacon.Nome + "-0";
            }
            List <ListaAdminB> lista = new List <ListaAdminB>();
            List <BA>          baaa  = new List <BA>();

            baaa = dataService.GetBAAsync();
            int max = 0;
            int i   = 0;

            for (i = 0; i < baaa.Count(); i++)
            {
                if (max < baaa[i].baID)
                {
                    max = baaa[i].baID;
                }
            }
            //lista = (List<ListaAdminB>)model2;
            bool a = await dataService.AddBeaconAsync(beacon, admin, men, flag);

            for (i = 0; i < model.list.Count(); i++)
            {
                if (model.list[i].addAdmin == true)
                {
                    max = max + 1;
                    BA ba = new BA();
                    ba.baID       = max;
                    ba.adminemail = model.list[i].Email;
                    ba.beaconID   = model.CreateBeacon.BeaconId;
                    bool verdade = dataService.AddBaAsync(ba);
                }
            }


            if (a == true)
            {
                Success(string.Format("<b>{0}</b> created with success!", beacon.beaconID), true);
                return(RedirectToAction("Index", "Home"));
            }

            ModelState.AddModelError("", "Beacon already registered.");


            return(View(model));
        }