How can get a list data of Google Sheets based on column names in Entity Framework - entity-framework

I'm modeling data search in Google Sheets using API (EF). I am currently connected to Google Sheets data. I also wrote a search based on RowId it's ok. Everything works fine. However I can't find data based on Id. Everything I have:
ItemGoogleSheet.cs
public class ItemGoogleSheet
{
public string Id { get; set; }
public string Name { get; set; }
}
ItemsGoogleSheetMapper.cs
public class ItemsGoogleSheetMapper
{
public static List<ItemGoogleSheet> MapFromRangeData(IList<IList<object>> values)
{
var items = new List<ItemGoogleSheet>();
foreach (var value in values)
{
ItemGoogleSheet item = new()
{
Id = value[0].ToString(),
Name = value[1].ToString(),
};
items.Add(item);
}
return items;
}
public static IList<IList<object>> MapToRangeData(ItemGoogleSheet item)
{
var objectList = new List<object>() { item.Id, item.Name };
var rangeData = new List<IList<object>> { objectList };
return rangeData;
}
}
ItemsGoogleSheetVATController.cs
public class ItemsGoogleSheetVATController : ControllerBase
{
const string SPREADSHEET_ID = "xxxx";
const string SHEET_NAME = "xx";
SpreadsheetsResource.ValuesResource _googleSheetValues;
public ItemsGoogleSheetVATController(GoogleSheetsHelper googleSheetsHelper)
{
_googleSheetValues = googleSheetsHelper.Service.Spreadsheets.Values;
}
[HttpGet("{rowId}")]
public IActionResult GetRowID(int rowId)
{
var range = $"{SHEET_NAME}!A{rowId}:AG{rowId}";
var request = _googleSheetValues.Get(SPREADSHEET_ID, range);
var response = request.Execute();
var values = response.Values;
return Ok(ItemsGoogleSheetMapper.MapFromRangeData(values).FirstOrDefault());
}
[HttpGet]
public IActionResult GetID(string id)
{
//How to get Data from Id
//return Ok();
}
}
My Google Sheets Data:
As in my description. I want to find Id = 0102 then it will output a list of results of: 0102, 01022101, 01022102
How can I get list of data based on Id column. Asking for any solutions from everyone. Thank you!

I have solved the problem. Thank you!

Related

REST API Custom Response query

I am a bit new to REST API. I have the below controller created to mock an API Service called by a client code under Test. I need to return the response in JSON format as mentioned in the query and need some help to fix it.
Controller:
[HttpPost]
[ApiKeyAuth]
[ValidateModel]
[ProducesResponseType(typeof(Item), StatusCodes.Status201Created)]
public async Task<IActionResult> AddNewItem([FromBody] Item item)
{
var itemId = await _repo.AddItemAsync(item);
return CreatedAtAction(nameof(GetItemById), new { itemId, controller = "Example" },
itemId);
}
Contract:
public interface IExampleControllerRepository
{
Task<int> AddItemAsync(Item item);
}
Repo:
public class ExampleRepository : IExampleControllerRepository
{
private readonly ExampleDbContext _context;
public ExampleRepository(ExampleDbContext context) => _context = context;
public async Task<int> AddItemAsync(Item item)
{
_context.Add(item);
await _context.SaveChangesAsync();
return item.ItemId;
}
}
Expected positive response template:
{"response":{"status":0,"data":[{"id":"1234"}]}}
// Summary:
// Creates a Microsoft.AspNetCore.Mvc.CreatedAtActionResult object that produces
// a Microsoft.AspNetCore.Http.StatusCodes.Status201Created response.
//
// Parameters:
// actionName:
// The name of the action to use for generating the URL.
//
// routeValues:
// The route data to use for generating the URL.
//
// value:
// The content value to format in the entity body.
//
// Returns:
// The created Microsoft.AspNetCore.Mvc.CreatedAtActionResult for the response.
[NonAction]
public virtual CreatedAtActionResult CreatedAtAction(string? actionName, object? routeValues, [ActionResultObjectValue] object? value)
{
throw null;
}
The response body's template is related to the type of value, So if you want to get the response template like:
{"response":{"status":0,"data":[{"id":"1234"}]}}
You need to pass a value of a specific type instead of itemId.
Here is a simple demo.
public class Test
{
public List<response> responses { get; set; }
}
public class response
{
public int status { get; set; }
public List<test1> data { get; set; }
}
public class test1
{
public string Id { get; set; }
}
For testing convenience, I just hard code here.
[HttpPost]
[ApiKeyAuth]
[ValidateModel]
[ProducesResponseType(typeof(Item), StatusCodes.Status201Created)]
public async Task<IActionResult> AddNewItem([FromBody] Item item)
{
Test test = new Test()
{
responses = new List<response>()
{
new response()
{
status = 0,
data = new List<test1> {
new test1()
{
Id = "1234",
}
}
}
}
};
var itemId = await _repo.AddItemAsync(item);
return CreatedAtAction(nameof(GetItemById), new { Id = item.ItemId }, test);
}
Result

.NET Core MongoDB. Find by guid returns null

I have the following setup:
The document:
[BsonCollection("Users")] // I get the collection name with a custom extension
[BsonIgnoreExtraElements]
public class UserDocument
{
[BsonId]
public Guid Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public UserSettingsModel UserSettings { get; set; }
}
public class UserSettingsModel
{
// ...
}
The repository:
public class UserRepository
{
private readonly IMongoCollection<UserDocument> _collection;
private readonly ILogger<UserRepository> _logger;
public UserRepository(IMongoDatabase database, ILogger<UserRepository> logger)
{
// returns "Users"
var collectionName = typeof(UserDocument).GetCollectionName();
_collection = database.GetCollection<UserDocument>(collectionName);
}
// ...
public async Task<UserDocument> GetById(Guid id)
{
var filter = Builders<UserDocument>.Filter.Eq(x => x.Id, id);
var user = await _collection.FindAsync(filter);
// var user = await _collection.FindAsync(x => x.Id == id); - doesn't work either
var request = filter.Render(
_collection.DocumentSerializer,
_collection.Settings.SerializerRegistry).ToString();
_logger.LogDebug(request);
return user.FirstOrDefault();
}
}
And I initialize the client this way:
// ...
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
var client = new MongoClient(connectionString);
var database = client.GetDatabase(dbName);
services.AddSingleton(c => database);
// convention pack and registries
// ...
// if moved here doesn't work either
// BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
The filter generated in GetById is still the following: { "_id" : CSUUID("459f165a-4a91-4f39-906c-dc7401ee2468") } when I expect it to be UUID instead of CSUUID.
So, the query doesn't find anything and returns null. In the database the document I'm searching for has _id: UUID('459f165a-4a91-4f39-906c-dc7401ee2468')
What am I doing wrong?
I was able to fixing by this trick:
var mongoConnectionUrl = new MongoUrl(connectionString);
var mongoClientSettings = MongoClientSettings.FromUrl(mongoConnectionUrl);
// before initializing client
mongoClientSettings.GuidRepresentation = GuidRepresentation.Standard;
However setting the GuidRepresentation in client settings is obsolete, which is quite confusing. Also, the query generated still has CSUUID instead of UUID. I was able to log the query the following way:
mongoClientSettings.ClusterConfigurator = cb =>
{
cb.Subscribe<CommandStartedEvent>(e => logger.LogDebug($"{e.CommandName} - {e.Command.ToJson()}"));
};
If anyone finds a better way and post it here it would be appreciated.

ServiceStack Ormlite - Postgres serializing Date property with MaxDate to JsonB

I have a complex object which I save to a JsonB field in postgres using Ormlite.
One of the property is a DateTime and is set to DateTime.Max.
Retrieving the object from Postgres the DateTime property value is set to DateTime.Min value
01/01/0001 00:00:00
Not sure if this is a bug with Ormlite or the json serializer.
Code snippet to replicate
class Program
{
static void Main(string[] args)
{
var item = new LicenseCheckTemp();
item.Body = new CheckHistory();
item.Body.List.Add(new ItemHistory() {AddedOn = DateTime.MaxValue, Note = "Test"});
var factory = GetFactory(ConfigurationManager.AppSettings["PostgresConnectionString"]);
using (var db = factory.OpenDbConnection())
{
db.CreateTableIfNotExists<LicenseCheckTemp>();
db.Save(item);
}
using (var db = factory.OpenDbConnection())
{
var items = db.Select<LicenseCheckTemp>();
foreach (var licenseCheck in items.OrderBy(x=>x.Id))
{
if (licenseCheck.Body != null && licenseCheck.Body.List.Any())
{
foreach (var itemHistory in licenseCheck.Body.List)
{
Console.WriteLine($"{itemHistory.AddedOn} : Note {itemHistory.Note}");
}
}
}
}
Console.ReadKey();
}
public static IDbConnectionFactory GetFactory(string connection)
{
var factory = new OrmLiteConnectionFactory(connection,
PostgreSqlDialect.Provider);
factory.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();
return factory;
}
}
public class LicenseCheckTemp
{
[AutoIncrement]
public int Id { get; set; }
[CustomField("json")]
public CheckHistory Body { get; set; }
}
public class CheckHistory
{
public List<ItemHistory> List { get; set; } = new List<ItemHistory>();
}
public class ItemHistory
{
public string Note { get; set; }
public DateTime AddedOn { get; set; }
}
Whilst OrmLite doesn't have explicit support for PostgreSQL JSON DataTypes, OrmLite's existing JSON serialization of ComplexType properties should allow this to work naturally as seen below:
I've added an example of this test in this commit:
OrmLiteConfig.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();
var item = new LicenseCheckTemp();
item.Body = new CheckHistory();
item.Body.List.Add(new ItemHistory { AddedOn = DateTime.MaxValue, Note = "Test" });
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<LicenseCheckTemp>();
db.GetLastSql().Print();
db.Save(item);
}
using (var db = OpenDbConnection())
{
var items = db.Select<LicenseCheckTemp>();
items.PrintDump();
foreach (var licenseCheck in items.OrderBy(x => x.Id))
{
if (licenseCheck.Body != null && licenseCheck.Body.List.Any())
{
foreach (var itemHistory in licenseCheck.Body.List)
{
$"{itemHistory.AddedOn} : Note {itemHistory.Note}".Print();
}
}
}
}
Which is working as expected, i.e. it Prints out:
CREATE TABLE "LicenseCheckTemp"
(
"Id" INTEGER PRIMARY KEY AUTOINCREMENT,
"Body" json NULL
);
[
{
Id: 1,
Body:
{
List:
[
{
Note: Test,
AddedOn: 9999-12-31T23:59:59.9999999-05:00
}
]
}
}
]
12/31/9999 11:59:59 PM : Note Test
Showing CreateTable creating a "json" type for Body property with the row being serialized and returned fully populated.
Can't repro why it's not working for you, are you using the latest v4.0.54 release of OrmLite? Does it work with a smaller DateTime? (perhaps Max DateTime in your TimeZone exceeds what your PgSql configured instance supports).

How do I patch enumerables with System.Web.Http.OData.Delta?

Trying to make use of System.Web.Http.OData.Delta to implement PATCH methods in ASP.NET Web API services, but it seems unable to apply changes to properties of type IEnumerable<T>. I'm using the latest Git revision of Delta (2012.2-rc-76-g8a73abe). Has anyone been able to make this work?
Consider this data type, which it should be possible to update in a PATCH request to the Web API service:
public class Person
{
HashSet<int> _friends = new HashSet<int>();
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public IEnumerable<int> Friends
{
get { return _friends; }
set
{
_friends = value != null ? new HashSet<int>(value) : new HashSet<int>();
}
}
public Person(int id, string firstName, string lastName)
{
Id = id;
FirstName = firstName;
LastName = lastName;
}
public Person()
{
}
}
This Web API method implements patching of a Person through Delta<Person>:
public void Patch(int id, Delta<Person> delta)
{
var person = _persons.Single(p => p.Id == id);
delta.Patch(person);
}
If I send a PATCH request with the following JSON to the service, the person's Friends property should be updated, but alas it doesn't happen:
{"Friends": [1]}
The crux of the matter is really how to make Delta update Friends with this data. See also the discussion at CodePlex.
The problem likely is that Deta will try to assign JSON's JArray to your Hashset<int>
If you are using it against JsonMEdiaTypeFormatter and you internalized the Delta code (meaning you can modify it), you'd have to do something like this (this is rough, but works):
Inside, bool TrySetPropertyValue(string name, object value) of Delta<T>, where it returns false:
if (value != null && !cacheHit.Property.PropertyType.IsPrimitive && !isGuid && !cacheHit.Property.PropertyType.IsAssignableFrom(value.GetType()))
{
return false;
}
Change to:
var valueType = value.GetType();
var propertyType = cacheHit.Property.PropertyType;
if (value != null && !propertyType.IsPrimitive && !propertyType.IsAssignableFrom(valueType))
{
var array = value as JArray;
if (array == null)
return false;
var underlyingType = propertyType.GetGenericArguments().FirstOrDefault() ??
propertyType.GetElementType();
if (underlyingType == typeof(string))
{
var a = array.ToObject<IEnumerable<string>>();
value = Activator.CreateInstance(propertyType, a);
}
else if (underlyingType == typeof(int))
{
var a = array.ToObject<IEnumerable<int>>();
value = Activator.CreateInstance(propertyType, a);
}
else
return false;
}
This will only work with collections of int or string but hopefully nudges you into a good direction.
For example, now your model can have:
public class Team {
public HashSet<string> PlayerIds { get; set; }
public List<int> CoachIds { get; set; }
}
And you'd be able to successfully update them.
You could override the TrySetPropertyValue method of the Delta class and make use of JArray class:
public sealed class DeltaWithCollectionsSupport<T> : Delta<T> where T : class
{
public override bool TrySetPropertyValue(string name, object value)
{
var propertyInfo = typeof(T).GetProperty(name);
return propertyInfo != null && value is JArray array
? base.TrySetPropertyValue(name, array.ToObject(propertyInfo.PropertyType))
: base.TrySetPropertyValue(name, value);
}
}
If you are using the ODataMediaTypeFormatter, this should be working. There are a couple of caveats to mention though.
1) your collections have to be settable.
2) the entire collection is replaced. you cannot remove/add individual elements.
Also, there is an issue tracking item 1 - '670 -Delta should support non-settable collections.'

Optgroup drop-down support in MVC - Problems with Model Binding

I wonder if anyone can shed some light on this problem..
I've got an option group drop-down for selecting a person's ethnicity – however it’s not storing the value in the model.
ViewModel
[UIHint("EthnicOriginEditorTemplate")]
[DisplayName("Question 6: Ethnic Origin")]
public int EthnicOrigin { get; set; }
Helper : GroupDropList.Cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
namespace Public.Helpers
{
public static class GroupDropListExtensions
{
public static string GroupDropList(this HtmlHelper helper, string name, IEnumerable<GroupDropListItem> data, int SelectedValue, object htmlAttributes)
{
if (data == null && helper.ViewData != null)
data = helper.ViewData.Eval(name) as IEnumerable<GroupDropListItem>;
if (data == null) return string.Empty;
var select = new TagBuilder("select");
if (htmlAttributes != null)
select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
select.GenerateId(name);
var optgroupHtml = new StringBuilder();
var groups = data.ToList();
foreach (var group in data)
{
var groupTag = new TagBuilder("optgroup");
groupTag.Attributes.Add("label", helper.Encode(group.Name));
var optHtml = new StringBuilder();
foreach (var item in group.Items)
{
var option = new TagBuilder("option");
option.Attributes.Add("value", helper.Encode(item.Value));
if (SelectedValue != 0 && item.Value == SelectedValue)
option.Attributes.Add("selected", "selected");
option.InnerHtml = helper.Encode(item.Text);
optHtml.AppendLine(option.ToString(TagRenderMode.Normal));
}
groupTag.InnerHtml = optHtml.ToString();
optgroupHtml.AppendLine(groupTag.ToString(TagRenderMode.Normal));
}
select.InnerHtml = optgroupHtml.ToString();
return select.ToString(TagRenderMode.Normal);
}
}
public class GroupDropListItem
{
public string Name { get; set; }
public List<OptionItem> Items { get; set; }
}
public class OptionItem
{
public string Text { get; set; }
public int Value { get; set; }
}
}
This is my EditorTemplate
<%# Import Namespace="Public.Helpers"%>
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int>"%>
<%=Html.GroupDropList("EthnicOrigin",
new[]
{
new GroupDropListItem
{
Name = "Ethnicity",
Items = new List<OptionItem>
{
new OptionItem {Value = 0, Text = "Please Select"}
}
},
new GroupDropListItem
{
Name = "a) White",
Items = new List<OptionItem>
{
new OptionItem {Value = 1, Text = "British"},
new OptionItem {Value = 2, Text = "Irish"},
new OptionItem {Value = 3, Text = "Other White (Please specify below)"}
}
},
--snip
}, Model, null)%>
And in the view I'm referencing it as:
<%=Html.EditorFor(x => x.EthnicOrigin, "EthnicOriginEditorTemplate")%>
However it's not passing through the selected Value into the model... has anyone experienced similar problems... many thanks in advance for some pointers.
Your select doesn't have a name attribute and so when you submit the form the selected value is not sent to the server. You need to add a name:
select.GenerateId(name);
select.MergeAttribute("name", name);
Just changed the helper class to get it work for MVC 3 and with nullable int.
Thanks a lot for the class, saves me plenty of time.
public static class GroupDropListExtensions
{
public static MvcHtmlString GroupDropList(this HtmlHelper helper, string name, IEnumerable<GroupDropListItem> data, int? SelectedValue, object htmlAttributes)
{
if (data == null && helper.ViewData != null)
data = helper.ViewData.Eval(name) as IEnumerable<GroupDropListItem>;
if (data == null) return new MvcHtmlString(string.Empty);
var select = new TagBuilder("select");
if (htmlAttributes != null)
select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
select.GenerateId(name);
select.MergeAttribute("name", name);
var optgroupHtml = new StringBuilder();
var groups = data.ToList();
foreach (var group in data)
{
var groupTag = new TagBuilder("optgroup");
groupTag.Attributes.Add("label", helper.Encode(group.Name));
var optHtml = new StringBuilder();
foreach (var item in group.Items)
{
var option = new TagBuilder("option");
option.Attributes.Add("value", helper.Encode(item.Value));
if (SelectedValue != 0 && item.Value == SelectedValue)
option.Attributes.Add("selected", "selected");
option.InnerHtml = helper.Encode(item.Text);
optHtml.AppendLine(option.ToString(TagRenderMode.Normal));
}
groupTag.InnerHtml = optHtml.ToString();
optgroupHtml.AppendLine(groupTag.ToString(TagRenderMode.Normal));
}
select.InnerHtml = optgroupHtml.ToString();
return new MvcHtmlString(select.ToString(TagRenderMode.Normal));
}
}
public class GroupDropListItem
{
public string Name { get; set; }
public List<OptionItem> Items { get; set; }
}
public class OptionItem
{
public string Text { get; set; }
public int Value { get; set; }
}
This is supported natively using SelectListGroup as of ASP.NET MVC 5.2:
var items = new List<SelectListItem>();
var group1 = new SelectListGroup() { Name = "Group 1" };
items.Add(new SelectListItem() { Text = "Item1", Group = group1 });
Then in MVC, do
#Html.DropDownList("select", items)