コード例 #1
0
        // POST: api/Core.svc/Jobs
        public async Task<IHttpActionResult> Post(Job job)
        {
            var declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
            var fn = String.Format("{0}:{1}",
                declaringType.Namespace,
                declaringType.Name);

            if (!ModelState.IsValid)
            {
                Debug.WriteLine("Job to be created has invalid ModelState.");
                return BadRequest(ModelState);
            }
            try
            {
                Debug.WriteLine(fn);

                var identity = CurrentUserDataProvider.GetIdentity(TenantId);

                var permissionId = CreatePermissionId("CanCreate");
                if (!identity.Permissions.Contains(permissionId))
                {
                    return StatusCode(HttpStatusCode.Forbidden);
                }
                if (null == job)
                {
                    var errorMsg = "Job to be created contains invalid data.";
                    Debug.WriteLine(errorMsg);
                    return BadRequest(errorMsg);
                }
                Debug.WriteLine("Saving new job...");

                var jobEntity = new Job()
                {
                    Created = DateTimeOffset.Now,
                    CreatedBy = identity.Username,
                    Tid = identity.Tid,
                    Type = null == job.Type ? "Default" : job.Type,
                    State = job.State,
                    Parameters = job.Parameters,
                    ReferencedItemId = job.ReferencedItemId,
                    Token = job.Token,
                    TenantId = job.TenantId
                };
                jobEntity = db.Jobs.Add(jobEntity);
                db.SaveChanges();
                Debug.WriteLine("Saved job with id '{0}'", jobEntity.Id);
                return ResponseMessage(ODataControllerHelper.ResponseCreated(this, jobEntity));
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", e.Source, e.Message, e.StackTrace));
                throw;
            }
        }
コード例 #2
0
        // PUT: api/Core.svc/Jobs(5)
        public async Task<IHttpActionResult> Put([FromODataUri] int key, Job job)
        {
            var declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
            var fn = String.Format("{0}:{1}",
                declaringType.Namespace,
                declaringType.Name);

            if (!ModelState.IsValid)
            {
                Debug.WriteLine("Job to be updated with id '{0}' has invalid ModelState.", key);
                return BadRequest(ModelState);
            }

            if (key != job.Id)
            {
                return BadRequest();
            }

            try
            {
                Debug.WriteLine(fn);

                var identity = CurrentUserDataProvider.GetIdentity(TenantId);

                var permissionId = CreatePermissionId("CanUpdate");
                if (!identity.Permissions.Contains(permissionId))
                {
                    return StatusCode(HttpStatusCode.Forbidden);
                }
                var original = db.Jobs.Find(job.Id);
                if (null == original)
                {
                    return StatusCode(HttpStatusCode.NotFound);
                }
                if (!CurrentUserDataProvider.IsEntityOfUser(identity.Username, identity.Tid, original))
                {
                    return StatusCode(HttpStatusCode.Forbidden);
                }
                job.Created = original.Created;
                job.CreatedBy = original.CreatedBy;
                job.Modified = DateTimeOffset.Now;
                job.ModifiedBy = identity.Username;
                job.Tid = original.Tid;
                db.Jobs.Attach(job);
                db.Entry(job).State = EntityState.Modified;
                db.SaveChanges();
                Debug.WriteLine("Job with id '{0}' updated", key);
                return Ok<Job>(job);
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", e.Source, e.Message, e.StackTrace));
                throw;
            }
        }