Giter Site home page Giter Site logo

leonardoporro / detached-patchtypes Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 1.0 165 KB

Create proxy types that tracks dirty properties to simulate 'undefined' values when deserializing json.

License: MIT License

C# 100.00%
partial-updates patch json undefined null proxy-types serializer mappers primitives

detached-patchtypes's People

Contributors

leonardoporro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bubdm

detached-patchtypes's Issues

I need some help with mapping related entities

For now my test action in abstract crud controller be like:

        [EnableQuery]
        [HttpPatch("/odata/[controller]({id:int})")]
        public virtual async Task<ActionResult<TEntity>> Patch(int id, [FromBody]TEntity delta, CancellationToken ctx)
        {
            var entity = PatchTypeFactory.Create<TEntity>();
            var type = typeof(TEntity);
            
            foreach (var property in type.GetProperties())
            {
                property.SetValue(entity, property.GetValue(delta));
            }
            entity.Id = id;
            await _db.MapAsync<TEntity>(entity);
            await _db.SaveChangesAsync(ctx);
            return Updated(entity);
        }

and entities are:

    public class User : IHasId
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual int Age { get; set; }
        public virtual int? RoleId { get; set; }
        [Aggregation]
        public virtual Role? Role { get; set; }
    }

    public class Role : IHasId
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        [Composition]
        public virtual List<User>? Users { get; set; }
        [Composition]
        public virtual List<Permission>? Permissions { get; set; }
    }

    public class Permission : IHasId
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        [Composition]
        public virtual List<Role>? Roles { get; set; }
    }

and when I send this request:

PATCH https://localhost:5001/odata/users(8)
Accept: application/json
OData-Version: 4.0
Content-Type: application/json;odata.metadata=full;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{
  "RoleId": 8,
  "Role": {
    "Id": 8,
    "Name": "RENAMED Role 8",
    "Permissions": [
      {
        "Id": 14,
        "Name": "RENAMED Permission 14"
      },
      {
        "Id": 15,
        "Name": "NEW Permission 15"
      }
    ]
  }
}

(and state of records in db: user exists without any role, role exists without any permissions, first permission exist and second permission not)

I'm expecting, that user #8 be related to role #8, role #8 be related to existed permission #14 and permission #15 will be created and related to role #8.

But for now only user #8 related to role #8...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.