Exemplo n.º 1
0
        public Responce <List <AppUsersDTO> > GetUsersListByCity(MobileRequest mobileRequest)
        {
            Responce <List <AppUsersDTO> > responce    = new Responce <List <AppUsersDTO> >();
            List <AppUsersDTO>             appUsersDTO = new List <AppUsersDTO>();

            try
            {
                responce.Success = true;
                using (SSCEntities db = new SSCEntities())
                {
                    List <UserDetailWithRole> UserList = new List <UserDetailWithRole>();
                    var  Users          = db.AspNetUsers.Find(mobileRequest.UserId);
                    var  UserRoles      = db.GetUserRole(mobileRequest.UserId).FirstOrDefault().UserRole;
                    bool UsersCityAdded = false;

                    foreach (Citys city in db.Citys.Where(a => a.AdminEmail == Users.UserName || UserRoles.Equals("Site Admin")).ToList())
                    {
                        if (Users.City.ToLower() == city.CityName.ToLower())
                        {
                            UsersCityAdded = true;
                        }

                        UserList = UserList.Concat(db.UserDetailWithRole.Where(a => a.City.ToLower() == city.CityName.ToLower()).ToList()).ToList();
                    }

                    if (!UsersCityAdded)
                    {
                        UserList = UserList.Concat(db.UserDetailWithRole.Where(a => a.City.ToLower() == Users.City.ToLower()).ToList()).ToList();
                    }

                    appUsersDTO = UserList?.Select(a => new AppUsersDTO()
                    {
                        Id = a.Id, Name = a.Name, Email = a.Email, City = a.City, Phone = a.Phonenumber, RoleId = a.RoleId, UserRole = a.UserRole
                    }).ToList();

                    //var Users = db.AspNetUsers.Find(mobileRequest.UserId);
                    //appUsersDTO = db.UserDetailWithRole.Where(a => a.City.ToLower() == Users.City.ToLower())?.Select(a => new AppUsersDTO() { Id = a.Id, Name = a.Name, Email = a.Email, City = a.City, Phone = a.Phonenumber, RoleId = a.RoleId, UserRole = a.UserRole }).ToList();

                    responce.ResponeContent = appUsersDTO;
                }
            }
            catch (Exception ex)
            {
                responce.Success        = false;
                responce.Message        = $"ERROR GetUsersList : {ex.InnerException}";
                responce.ResponeContent = appUsersDTO;
            }
            return(responce);
        }
#pragma warning disable CS1998 // This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
        public async Task <Responce <List <ComplaintsDTO> > > GetComplaint(MobileRequest mobileRequest)
#pragma warning restore CS1998 // This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
        {
            Responce <List <ComplaintsDTO> > Responce = new Responce <List <ComplaintsDTO> >();

            Responce.Success = true;
            try
            {
                List <ComplaintsDTO> Complaints     = new List <ComplaintsDTO>();
                List <Complaints>    ComplaintsList = new List <Complaints>();

                using (SSCEntities db = new SSCEntities())
                {
                    if (mobileRequest.Id != 0)
                    {
                        var Getcomplaintbyid = db.Complaints.Find(mobileRequest.Id);
                        if (Getcomplaintbyid != null)
                        {
                            ComplaintsList.Add(Getcomplaintbyid);
                        }
                    }
                    else
                    {
                        var UserCity  = db.AspNetUsers.Find(mobileRequest.Username).City;
                        var UserName  = db.AspNetUsers.Find(mobileRequest.Username).UserName;
                        var CityId    = db.Citys.Where(a => a.CityName.ToLower().Equals(UserCity.ToLower()))?.FirstOrDefault().Id;
                        var UserRoles = db.GetUserRole(mobileRequest.Username).FirstOrDefault().UserRole;

                        switch (UserRoles)
                        {
                        case "Volunteer":
                            ComplaintsList = db.Complaints.Where(a => a.UserId.Trim().ToLower() == mobileRequest.Username.Trim().ToLower()).ToList();
                            break;

                        case "Controller":
                            ComplaintsList = db.Complaints.Where(a => a.City == CityId).ToList();
                            break;

                        case "Admin":
                            foreach (Citys city in db.Citys.Where(a => a.AdminEmail == UserName || a.Id == CityId).ToList())
                            {
                                ComplaintsList = ComplaintsList.Concat(db.Complaints.Where(a => a.City == city.Id).ToList()).ToList();
                            }
                            break;

                        case "Site Admin":
                            ComplaintsList = db.Complaints.ToList();
                            break;
                        }

                        //if (UserRoles.Equals("Volunteer"))
                        //{
                        //ComplaintsList = db.Complaints.Where(a=>a.City==CityId && a.UserId.Trim().ToLower()== mobileRequest.Username.Trim().ToLower()).ToList();

                        //}
                        //else
                        //{
                        //        ComplaintsList = db.Complaints.Where(a => a.City == CityId).ToList();
                        //}
                    }

                    Complaints = ComplaintsList?.Select(a => a.MapComplaintsToDTO()).OrderByDescending(a => a.ModifiedDate).OrderBy(a => a.ComplainStatus).ToList();
                    Responce.ResponeContent = Complaints;
                }
            }
            catch (Exception ex)
            {
                Responce.Success = false;
                Responce.Message = $"ERROR GetComplaint :{ex.ToString()}";
            }
            return(Responce);
        }