Why does swagger gives me ISE error when i return return JSON - asp.net-core

I have the following code im not sure why swagger doesnt like it, i get this error
public JsonResult ProductsForTreeView(int? id)
{
var employees = from e in Context.ResponsibleParty
where (id.HasValue ? e.ProductKey == id : e.ProductKey == 0)
select new
{
id = e.ID,
Name = e.PartyName + " " + e.ProductCode,
hasChildren = (from q in Context.Products
where (q.ID == e.ProductKey)
select q
).Count() > 0
};
return Json(employees.ToList());
}
I get this error
Fetch errorInternal Server Error /swagger/v1/swagger.json

Related

I'm using 3 filters in my lightning component to want to show data all together,i.e. record should be displayed after filtering through all filters

I have created a lightning component with 3 filters- one for searching listview, one from dropdown (picklist) and third by searching the name. I'm able to make it work by individual search but for combining have been asked to use dynamic SQL in apex by passing the string in apex class. So here's code is attached of class .But I don't know how to move forward.
public with sharing class ApexSearchForm {
#AuraEnabled
public static List<Asset__c> fetchAccounts(String status, String v) {
String a= 'Recently Viewed Assets';
String b='All Assets';
String c='Choose One';
List<Asset__c> accList = new List<Asset__c>();
if(String.isNotBlank(status)){
accList = [SELECT Name, Status__c, Description__c, Executive_Sponsor__c, Documentation_Link__c,Video_Link__c,Source_Code_Link__c
FROM Asset__c
WHERE Status__c = :status];
}if(String.isBlank(status)){
accList = [SELECT Name, Status__c, Description__c, Executive_Sponsor__c, Documentation_Link__c, Video_Link__c,Source_Code_Link__c
FROM Asset__c LIMIT 100];
}if(v == a){
accList = [SELECT Id, Name, Description__c, Submitted_Date__c, Documentation_Link__c, Source_Code_Link__c,Video_Link__c,Status__c
FROM Asset__c
WHERE LastViewedDate != NULL
ORDER BY LastViewedDate DESC LIMIT 5];
}else if(v==b){
accList = [SELECT Id, Name, Description__c, Submitted_Date__c, Documentation_Link__c, Source_Code_Link__c,Video_Link__c,Status__c
FROM Asset__c
ORDER BY Name ASC ];
}else if(v==c){
accList = [SELECT Id, Name, Description__c, Submitted_Date__c, Documentation_Link__c, Source_Code_Link__c,Video_Link__c,Status__c
FROM Asset__c ];
}else if(String.isNotBlank(status) && v==a){
accList=[SELECT Id, Name, Description__c, Submitted_Date__c, Documentation_Link__c, Source_Code_Link__c,Video_Link__c,Status__c
FROM Asset__c
WHERE Status__c = :status
ORDER BY LastViewedDate DESC];
}
return accList;
}
#AuraEnabled
public static Map<String, String> getStatusFieldValue(){
Map<String, String> options = new Map<String, String>();
Schema.DescribeFieldResult fieldResult = Asset__c.Status__c.getDescribe();
List<Schema.PicklistEntry> pValues = fieldResult.getPicklistValues();
for (Schema.PicklistEntry p: pValues) {
options.put(p.getValue(), p.getLabel());
}
return options;
}
}
Assuming that you pass correctly the parameters from frontend to the controller, you can do something like:
public static List<Asset__c> fetchAccounts(String status, String v) {
String a= 'Recently Viewed Assets';
String b='All Assets';
String query = 'SELECT Name, Status__c, Description__c, Executive_Sponsor__c, Documentation_Link__c,Video_Link__c,Source_Code_Link__c FROM Asset__c';
if(String.isNotBlank(status) || v != null){
query += ' WHERE';
if(String.isNotBlank(status)){
query += ' Status__c = :status';
}
if(v != null){
query += ' ORDER BY';
if(v == a){
query += ' LastViewedDate DESC';
if(String.isNotBlank(status)){
query += ' LIMIT 5';
}
} else if(v == b){
query += ' Name ASC';
}
}
} else {
query += ' LIMIT 100';
}
List<Asset__c> accList = Database.query(query);
return accList;
}

DBContext Query returns null

Codes inside Weekly returns a value. But in Monthly it returns null.
I tried to run the query in my SQL Server Management Studio and it returns the correct result. But in my code, it returns null.
I tried to breakpoint and the item.JobId is = 2.
Here is my codes.
if (item.Frequency == "Weekly")
{
if (item.StartDate.DayOfWeek.ToString() == DateTime.Now.DayOfWeek.ToString())
{
var jobname = _context.Jobs.Where(x => x.Id == item.JobId).FirstOrDefault();
if (ModelState.IsValid)
{
jobs.Id = 0;
jobs.Job = jobname.Job;
jobs.ClientName = jobname.ClientName;
jobs.ClientId = jobname.ClientId;
jobs.JobRate = jobname.JobRate;
jobs.Status = "Active";
}
}
}
else if (item.Frequency == "Monthly")
{
if (item.StartDate.Day.ToString() == DateTime.Now.Day.ToString())
{
var jobname = _context.Jobs.Where(x => x.Id == item.JobId).FirstOrDefault();
var a = jobname.Job;
if (ModelState.IsValid)
{
jobs.Id = 0;
jobs.Job = jobname.Job;
jobs.ClientName = jobname.ClientName;
jobs.ClientId = jobname.ClientId;
jobs.JobRate = jobname.JobRate;
jobs.Status = "Active";
}
}
}
And here is my SQL Query
SELECT TOP (200) Id, Job, ClientName, ClientId, JobRate, Status
FROM Jobs
WHERE (Id = 2)
It should return some value in it.
Both inner expressions of the ifs(ones controlling item.StartDate) are identical, so your problem lies in these controls. Can you make sure your code evaluates true for below condition?
if (item.StartDate.Day.ToString() == DateTime.Now.Day.ToString())

how to Use UNION and Order by Clause in my JPQL Query

I am using eclipse link as a JPA in my project. the current jar i am using is eclipselink-2.5.2.Coming to my Question,
I want to use Order by in UNION combination. My JPQL query looks like following
select req from Rqst req
WHERE (req.Applc =:SYSTEM_IDENTIFIER1 AND req.procesTyp =:PROCESS_TYPE1 AND UPPER(req.updtBy) =:UPDATED_BY1)
AND req.stat <> :ignoreDeletedRequests
UNION
select req from Rqst req
WHERE (req.Applc =:SYSTEM_IDENTIFIER2 AND req.procesTyp =:PROCESS_TYPE2 AND UPPER(req.updtBy) =:UPDATED_BY2)
AND req.stat <> :ignoreDeletedRequests
UNION
select req from Rqst req
WHERE (req.Applc =:SYSTEM_IDENTIFIER3 AND req.procesTyp =:PROCESS_TYPE3 AND UPPER(req.updtBy) =:UPDATED_BY3)
AND req.stat <> :ignoreDeletedRequests
ORDER BY req.rqstId ASC
With out Order by it is working for me. But when i use Order by I am getting The query contains a malformed ending.
Can any help me here i am stuck for long time.
I had the problem as you. I solved that problem by using Collection.sort() like that:
Collections.sort(listObjects, new Comparator<Object[]>() {
#Override
public int compare(final Object[] record1, final Object[] record2) {
int c;
c = convertTime(record1[0].toString()).compareTo(convertTime(record2[0].toString()));
if (c == 0) {
c = record1[1].toString().compareTo(record2[1].toString());
}
if (c == 0) {
c = record1[2].toString().compareTo(record2[2].toString());
}
if (c == 0) {
c = new Long(Long.parseLong(record1[3].toString())).compareTo(Long.parseLong(record2[3].toString()));
}
if (c == 0) {
c = ((Date) record1[4]).compareTo(((Date) record2[4]));
}
if (c == 0) {
c = ((Date) record1[5]).compareTo(((Date) record2[5]));
}
if (c == 0) {
c = new Long(Long.parseLong(record1[6].toString())).compareTo(Long.parseLong(record2[6].toString()));
}
return c;
}
});
You can use Collention.sort() to order multiple fields;

Entity Framework Foreign Key Connection

Here's my controller :
public ActionResult Display(int id)
{
Child child = db.Children.SingleOrDefault(c => c.ChildID == id);
ViewBag.ChildName = child.FamilyName + ", " + child.GivenName + ", " + child.MiddleName;
EducationalHistory eduHist = new EducationalHistory();
eduHist.ChildID = id;
child.EducationalHistories.Add(eduHist);
return PartialView(child);
}
I actually tried to inner join this into multiple tables and I've checked the query it seems correct here's the code:
var chs = from eh in db.EducationalHistories
from shl in db.SchoolLevels
from shy in db.SchoolYears
from ar in db.Areas
from sh in db.Schools
from gr in db.GradeLevels
where eh.SchoolLevelID == shl.SchoolLevelID
where eh.SchoolYearID == shy.SchoolYearID
where eh.AreaID == ar.AreaID
where eh.SchoolID == sh.SchoolID
where eh.GradeLevelID== gr.GradeLevelID
select new
{
eh.ChildID,
shl.SchoolLevelName,
shy.SchoolYearName,
ar.AreaName,
sh.SchoolName,
gr.GradeLevelName
};
ViewBag.EducationalList = chs;
EducationalHistory eduHist = new EducationalHistory();
eduHist.ChildID = id;
// child.EducationalHistories.Add(eduHist);
// ViewBag.SelectList = db.EducationalHistories.Find(id);
return PartialView(chs);
But my problem now is I don't know how to transfer the data from CHS variable to the partial View.
I mostly past a model value on the view model using a model class. Could I use the viewbag to transfer the queried data ?
Thank you again.

How to create a Dynamic Query when there's a condition that won't use all fields in the where section with LinQ

I am using a Data Entity from the db, and a Domain Service.
I am using .net's generated code for the simple queries, like this
public IQueryable<employee> GetEmployeesByLocale(int localeID)
{
return ObjectContext.employees.Where(e => e.Locale_ID == localeID);
}
Now, I need to change the .Where section accordingly, so:
if (localeID > 0)
{
['Where' should be like .Where(e => e.Locale_ID == localeID)];
}
if (projectID > 0)
{
[IF localeID == 0, 'Where' should be like .Where(e => e.Project_ID == projectID)
Else if localeID > 0, Where should use both, sort of .Where(e => e.Locale_ID == localeID && e.Project_ID == projectID)];
}
and so on with other variables.
There are many possible combinations , which is why I was trying to use the overload for .Where(string, parameter[])
string q = string.Empty;
if (localeID > 0)
{
q = "Locale_ID = " + localeID.ToString();
}
if (projectID > 0)
{
q = q == string.Empty ? "Project_ID = " + projectID.ToString() : q + " and " + "Project_ID = " + projectID.ToString();
}
... (for other variables and fields)
...
System.Data.Objects.ObjectParameter[] param = new System.Data.Objects.ObjectParameter[1];
param[0] = new System.Data.Objects.ObjectParameter("param", 1);
return ObjectContext.employees.Where(q, param);
However, this only gives an error, because then all the fieldnames are supposedly out-of-scope/non-existing. Even if use employees.[field_name] in the string, they "don't exist"
Does anyone know of a way to use conditionals inside the .Where part of the query? Or how to create some var or object containing my query so I can just parse it?
You can use
IQueryable<Employee> emp = ObjectContext.employees;
if (localeID > 0)
emp=emp.Where(e => e.Locale_ID == localeID).AsQueryable();
if (projectID > 0)
emp=emp.Where(e => e.Project_ID == projectID).AsQueryable();
This will concatenate the where clauses