I'm trying to count Order from Database with API but it doesn't seem to work.
There's a message from Swagger that said "Failed to load API definition. Fetch error undefined swagger/v1/swagger.json"
This is in OrderController:
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))]
public async Task<ActionResult<int>> CountOrders()
{
int result = await _orderService.CountOrders();
return Ok(result);
}
This is in OrderService:
public async Task<int> CountOrders()
{
return _orderRepository.CountOrder();
}
This is in OrderRepository:
public int CountOrder()
{
IQueryable<DataProvider.Entities.Order> orderList;
orderList = GetAll();
return orderList.Count();
}
I'm new to this so please point out if there's anything makes you feel uncomfortable. Thanks.
I solved it. I was kind of stupid and didn't realise I haven't change the route, so it got duplicated.
It works after adding [Route("count")]
Related
I'm new in Angular, and I'm currently into a big project where many people work, so the project is based on Angular for the front end and C# for the back end.
So, the project has some services that call the backend service:
Front:
public editProfileClient(profileClient) {
return this.http
.post(this.url + '/editProfileClient', profileClient)
.pipe(map((result: ProfileClientModel) => result));
}
Back:
public async Task<ActionResult> EditProfileClient(ProfileClient profileClient)
{
//irrelevant code here
return Ok(model);
}
This is working well, but now I want to send a new model called Salary to that request, so I changed the back as:
public async Task<ActionResult> EditProfileClient(ProfileClient profileClient, Salary salary)
but I have no idea how I can send it on the front, so I receive it, but I cannot call it:
public editProfileClient(profileClient, salary) {
return this.http
.post(this.url + '/editProfileClient', profileClient, salary)
.pipe(map((result: ProfileClientModel) => result));
}
If I try to do that, the method returns an error:
Argument of type 'OperatorFunction<ProfileClientModel,
ProfileClientModel>' is not assignable to parameter of type
'OperatorFunction<ArrayBuffer, ProfileClientModel>'.
How can I achieve that?
For the API part, combine both parameters into a single object as below:
public async Task<ActionResult> EditProfileClient(EditProfileClientInputModel input)
public class EditProfileClientInputModel
{
public ProfileClient ProfileClient { get; set; }
public Salary Salary { get; set; }
}
For the front-end part:
2.1. Combine both profileClient and salary parameters into a single object and pass it.
2.2. As your API returns the response of ProfileClientModel type, you should also specify the generic type: post<T>()
public editProfileClient(profileClient, salary) {
let input = {
profileClient: profileClient,
salary: salary
};
return this.http
.post<ProfileClientModel>(this.url + '/editProfileClient', input);
}
Update
.pipe(map((result: ProfileClientModel) => result))
As per Eliseo's feedback, the pipe() should be removed as map() is used to transform the data, while you try to transform the value into the same value, which is unnecessary.
I am using the repository pattern with the .Net Core Web Application that communicates with .Net Core Web API Controller Methods to get records from SqlServer.
On the Web API Controller method, I am calling a method in the repository which must return a list of distinct messages based on a specific field in the Sql Server database like PhoneNumberTo. However, there seem to be some casting errors when I am returning the list back to the API method. Here is my Web API Controller method which calls the repository:
[HttpGet]
[Route("GetUniqueMessages")]
public IActionResult GetUniqueMessages()
{
var messages = _unitOfWork.MessagesViewModel.GetDistinctMessages();
return new JsonResult(messages);
}
And here is my repository method which must return the list of distinct messages:
public IEnumerable<Message> GetDistinctMessages()
{
List<Message> result = _context.Messages.Select(m => m.PhoneNumberTo).Distinct().ToList();
return result;
}
Here is a screenshot of the error message I am getting on the repository method.
Update:
Here is my Message class:
Any expert suggestion will highly be appreciated.
you have two choices.
If you need to get a list of phone numbers only
public IEnumerable<string> GetDistinctMessages()
{
return _context.Messages.Select(m => m.PhoneNumberTo).Distinct().ToArray();
}
If you need a list of messages, you can't have have all distinct fields, it will be the same like this
public IEnumerable<Message> GetDistinctMessages()
{
return _context.Messages.Distinct().ToArray();
}
I offer you to get MessageTo MessageFrom and MessageText and see the difference
public IEnumerable<Message> GetDistinctMessages()
{
return _context.Messages
.GroupBy(p => new {p.MessageTo, p.MessageFrom} )
.Select(g => new Message
{
MessageTo = g.Key.MessageTo,
MessageFrom = g.Key.MessageFrom,
MessageText= g.MessageText
}).ToArray();
You can play with this query and add as many fields as you need
you can still keep an existing action, but I recomend this
[HttpGet]
[Route("GetUniqueMessages")]
public IActionResult GetUniqueMessages()
{
var messages = _unitOfWork.MessagesViewModel.GetDistinctMessages();
return Ok(messages);
}
I'm fairly new to WEB API so please forgive the ignorance. I'm trying to return a list of events that a specific user will be attending and nothing more. My code works but it returns more info than I need. This is what is returned when I call the API : [{"$id":"1","eventID":"1"},{"$id":"2","eventID":"2"}]
My controller code is as follows:
public HttpResponseMessage Get(string id)
{
List<GetEventAttend> events = null;
events = db.userattends.Where(x => x.userID == id).Select(s => new GetEventAttend()
{ eventID = s.eventID }).ToList<GetEventAttend>();
return Request.CreateResponse(HttpStatusCode.OK, events);
}
This is the code for GetEventAttend:
public class GetEventAttend
{
public string eventID { get; set; }
}
Is there any way I can return in the format of {"1","2"}?
You're almost there but instead of selecting a new GetEventAttend you can just select the eventID fields and return those:
public HttpResponseMessage Get(string id)
{
var events = db.userattends.Where(x => x.userID == id).Select(s => s.eventID).ToList();
return Request.CreateResponse(HttpStatusCode.OK, events);
}
Is the GetEventAttend class really that small or is that just for demo purposes? If it is just a container for the web api result than you do not need that class, as the answer shows you.
EDIT: CodeCaster has a point. This answer will return an array of eventIds. This might suffice for now but in later stages you might want to return an array of events, even if they consist of just the identifier. Because now if you want to include additional information about the event you have to create a new api or introduce breaking changes.
In the original code you have probably configured reference handling, see the docs on how to disable that:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.None;
I wrote database on Entity Framework Code First and i use it in my Web Api.
If i have more than 400 records in my table ,i can`t get any records, but if i have less than 400 record everything works ok . This is my code for getting records :
public IEnumerable<Contact> Get(int pageSize, int pageIndex)
{
return db.Set<Contact>().OrderBy(e => e.Id).Skip(pageSize * pageIndex).Take(pageSize).AsParallel<Contact>().AsOrdered();
}
Another method for getting 1 record doesnt work too.
public Contact Get(int id) { return db.Set<Contact>().Find(id); }
And i notice with adding new records times for response increments.
When i have 100 record time for response is 4 second,if i have 200 second 10 seconds.
Can you explain what i am doing wrong ?
EDIT: code from controller
private ICompanyService companyService;
public CompaniesController(ICompanyService companyService)
{
this.companyService = companyService;
}
//// GET api/companies
public IHttpActionResult Get([FromUri]int SizePage=100, [FromUri]int Page=1)
{
return Ok(companyService.Get(SizePage, Page));
}
//// GET api/companies/5
public IHttpActionResult Get([FromUri]int id)
{
return Ok(companyService.Get(id));
}
I am trying to update a specific part with following code:
var nationalPart = _contentManager.Get<NationalPart>(Id);
nationalPart.Name = part.Name;
i have already done this.but now it is not working.even i call contentManager.Flush() explicitly, but still no luck. can anyone help me with this?
thanks in advance.
EDIT: i found where my problem originates from!
when i make a JSON request to update a contentPart.the in memory version is updating but it is not reflecting the result to db.now we are one step closer to answer.but what is preventing the session from updating the db?
EDIT2:
this is code for my handler :
public NationalPartHandler(IRepository<NationalPartRecord> repository)
{
Filters.Add(StorageFilter.For(repository));
}
this is code for Controller Action:
[GridAction(EnableCustomBinding = true)]
public JsonResult UpdateNational(NationalViewModel Model, GridCommand Command)
{
if (!ModelState.IsValid)
return new JsonResult { Data = "error" };
var NationalPart = _contentmanager.Get(Model.Id).As<NationalPart>();
NationalPart.Record = new NationalPartRecord();
NationalPart.Record.MapFrom<NationalPartRecord, NationalViewModel>(Model);
_soccerService.UpdateNational(NationalPart);
return NationalsListJson(Command);
}
and finally code for service:
public void UpdateNational(NationalPart part)
{
var nationalPart = _contentManager.Get<NationalPart>(part.Id);
nationalPart.Name = part.Name;
_contentManager.Flush();
}
Remove the line
NationalPart.Record = new NationalPartRecord();
The underlying record is automatically generated and is proxied so NHibernate can save the data to the database whenever you do any change to it. You don't want to replace that ever, otherwise no data will get saved.
UpdateNational is redundant and you can remove it - you already have part.Name (which if I understand correctly, should map to part.Record.Name) set during call to record.MapFrom.
So the correct version would look like this
[GridAction(EnableCustomBinding = true)]
public JsonResult UpdateNational(NationalViewModel Model, GridCommand Command)
{
if (!ModelState.IsValid)
return new JsonResult { Data = "error" };
var part = _contentmanager.Get(Model.Id).As<NationalPart>();
part.Record.MapFrom<NationalPartRecord, NationalViewModel>(Model);
return NationalsListJson(Command);
}
given your NationalPart looks similar to:
public NationalPart : ContentPart<NationalPartRecord>
{
public string Name {
get { return Record.Name; }
set { Record.Name = value; }
}
}
Summarizing - if you want to store some data in Orchard, all you need to do is to set a value of some property of a record, nothing more. Data will get persisted at the end of a request (or earlier, if you call IContentManager.Flush()).