Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4102

Unable to set a value from 1 model to another using a "Put" method in Postman

$
0
0

I have already created a working register user method in which a user is registered to the database. I have also created a working new package method which creates a new package. They both work fine however when a user is created his account type is set to null, I simply want to set the users "accountType" to be the "PackageName".

Package Model:

public class Packages    {        public int Id { get; set; }        public string PackageName { get; set; }        public int OrderLimit { get; set; }        public decimal Cost { get; set; }        public string ResetIntervalType { get; set; }        public int ResetInterval { get; set; }}

User Model:

 public class AppUser : IdentityUser     {        public string FirstName { get; set; }        public string LastName { get; set; }        public string AccountType { get; set; }        public int? PeriodOrderQty { get; set; }        public int? TotalOrderQty { get; set; }        public Guid? APIKey { get; set; }        //added packages foreign key, connection established        public Packages Package { get; set; }        public Cities City { get; set; }        public QualificationLevels Qualifications { get; set; }    }

Here is the put method i'm attempting to implement, after break pointing each component the main problem is the mapper being set towards the start of the method. However i'm not sure what else to try, only just started using these sort of methods.

[HttpPut("api/auth/assignpackage/{id}")]            public async Task<IActionResult> AssignPackage(int id ,[FromBody]Packages packageAssignValue)            {                try                {                    var assignPackage = packageAssignValue;                    var assignUser = Mapper.Map<Packages, AppUser>(packageAssignValue);                    assignUser.AccountType = packageToEdit.PackageName;                    var packageToEdit = await _context.Packages                      .AsNoTracking()                      .SingleOrDefaultAsync(m => m.Id == id);                    assignUser.AccountType = assignPackage.PackageName;                    if (packageToEdit == null)                    {                        return NotFound("Could not update Package as it was not Found");                    }                    else                    {                        _context.Update(assignUser);                        await _context.SaveChangesAsync();                        return Ok(assignUser);                    }                }                catch (DbUpdateException)                {                    //Log the error (uncomment ex variable name and write a log.)                    ModelState.AddModelError("", "Unable to save changes. "+"Try again, and if the problem persists, "+"see your system administrator.");                    return NotFound("Package not found");                }            }

For reference here is my postman input:

{"packageName": "Bronze"}

Viewing all articles
Browse latest Browse all 4102

Trending Articles