Exemplo n.º 1
0
        public async Task <IHttpActionResult> PostProfileMaterial(ProfileMaterialDTO profileMaterial)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userId = User.Identity.GetUserId();

            if (string.IsNullOrEmpty(profileMaterial.ProfileId))
            {
                profileMaterial.ProfileId = userId;
            }
            if (profileMaterial.ProfileId != userId)
            {
                return(Unauthorized());
            }

            var profMat = AutoMapper.Mapper.Map <ProfileMaterial>(profileMaterial);

            db.ProfileMaterials.Add(profMat);
            await db.SaveChangesAsync();

            profMat.Material = await db.Materials.FindAsync(profMat.MaterialId);

            return(CreatedAtRoute("GetProfileMaterialById", new { id = profMat.Id },
                                  AutoMapper.Mapper.Map <ProfileMaterialDTO>(profMat)));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> GetProfileMaterial(int id)
        {
            var userId = User.Identity.GetUserId();
            ProfileMaterialDTO profileMaterial = AutoMapper.Mapper.Map <ProfileMaterialDTO>(
                await db.ProfileMaterials
                .Where(pm => pm.Id == id && pm.ProfileId == userId)
                .FirstOrDefaultAsync());

            if (profileMaterial == null)
            {
                return(NotFound());
            }

            return(Ok(profileMaterial));
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> PutProfileMaterial(int id, ProfileMaterialDTO profileMaterial)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != profileMaterial.Id)
            {
                return(BadRequest());
            }

            var userId = User.Identity.GetUserId();

            if (string.IsNullOrEmpty(profileMaterial.ProfileId))
            {
                profileMaterial.ProfileId = userId;
            }
            if (profileMaterial.ProfileId != userId)
            {
                return(Unauthorized());
            }

            db.Entry(AutoMapper.Mapper.Map <ProfileMaterial>(profileMaterial)).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProfileMaterialExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }