Example #1
0
        public IActionResult Update(int id, [FromBody] UpdatePackageOptions options)
        {
            var result = packageService_.UpdatePackage(id, options);

            if (!result.Success)
            {
                return(StatusCode((int)result.ErrorCode, result.ErrorText));
            }

            return(Json(result));
        }
Example #2
0
        public Result <bool> UpdatePackage(UpdatePackageOptions options, int id)
        {
            var result = new Result <bool>();

            if (options == null)
            {
                result.ErrorCode = StatusCode.BadRequest;
                result.ErrorText = "Null options";
                return(result);
            }

            var package = dbContext.Set <Package>()
                          .Where(p => p.PackageId == id)
                          .SingleOrDefault();

            if (package == null)
            {
                result.ErrorCode = StatusCode.NotFound;
                result.ErrorText = $"Package with id {id} was not found";
                return(result);
            }

            if (!string.IsNullOrWhiteSpace(options.Description))
            {
                package.Description = options.Description;
            }

            if (dbContext.SaveChanges() > 0)
            {
                result.ErrorCode = StatusCode.OK;
                result.Data      = true;
                return(result);
            }

            result.ErrorCode = StatusCode.InternalServerError;
            result.ErrorText = $"Package could not be updated";
            return(result);
        }
        public ApiResult <bool> UpdatePackage(int?id, UpdatePackageOptions options)
        {
            if (options == null)
            {
                return(ApiResult <bool> .Failed(StatusCode.BadRequest, "Null options"));
            }

            if (id == null)
            {
                return(ApiResult <bool> .Failed(StatusCode.BadRequest, "The Package Id is empty"));
            }

            var updatePackage = GetPackageById(id).Data;

            if (options.Description != null)
            {
                updatePackage.Description = options.Description;
            }

            if (options.Reward != null)
            {
                updatePackage.Reward = options.Reward.Value;
            }

            if (options.IsActive != null)
            {
                updatePackage.IsActive = options.IsActive.Value;
            }

            var project = context_
                          .Set <Project>()
                          .Where(p => p.ProjectId == options.ProjectId)
                          .Include(x => x.Packages)
                          .SingleOrDefault();

            foreach (var item in project.Packages)
            {
                if (item.Description == options.Description && item.Reward == options.Reward)
                {
                    return(ApiResult <bool> .Failed(StatusCode.BadRequest, "Package with the same options already exists"));
                }
            }

            project.Packages.Add(updatePackage);
            context_.Update(project);

            try
            {
                var rows = context_.SaveChanges();
                if (rows <= 0)
                {
                    return(ApiResult <bool> .Failed(
                               StatusCode.InternalServerError, "Package could not be updated"));
                }
            }
            catch (Exception ex)
            {
                return(ApiResult <bool> .Failed(StatusCode.InternalServerError, ex.ToString()));
            }

            return(ApiResult <bool> .Successful(true));
        }