예제 #1
0
        public Photo CreatePhoto(CreatePhotoOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var project = projectService_.Search(new SearchProjectOptions
            {
                ProjectId = options.ProjectId
            }).SingleOrDefault();

            if (project == null)
            {
                return(null);
            }

            var photo = new Photo()
            {
                Name = options.Name,
                Path = options.Path,
            };

            project.Photos.Add(photo);
            context_.Update(project);
            context_.Add(photo);

            if (context_.SaveChanges() > 0)
            {
                return(photo);
            }

            return(null);
        }
예제 #2
0
        public Result <Photo> CreatePhoto(int projectId,
                                          CreatePhotoOptions options)
        {
            if (options == null)
            {
                return(Result <Photo> .ActionFailed(
                           StatusCode.BadRequest, "Null options"));
            }

            if (string.IsNullOrWhiteSpace(options.Url))
            {
                return(Result <Photo> .ActionFailed(
                           StatusCode.BadRequest, "Null or empty Url"));
            }

            var project = context_
                          .Set <Project>()
                          .Where(p => p.ProjectId == projectId)
                          .SingleOrDefault();

            if (project == null)
            {
                return(Result <Photo> .ActionFailed(
                           StatusCode.BadRequest, "Invalid ProjectId"));
            }

            var photo = new Photo()
            {
                Url = options.Url
            };

            project.Photos.Add(photo);

            var rows = 0;

            try
            {
                rows = context_.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Result <Photo> .ActionFailed(
                           StatusCode.InternalServerError, ex.ToString()));
            }

            if (rows <= 0)
            {
                return(Result <Photo> .ActionFailed(
                           StatusCode.InternalServerError,
                           "Photo could not be added"));
            }

            return(Result <Photo> .ActionSuccessful(photo));
        }
        public IActionResult Create(int id, [FromBody] CreatePhotoOptions options)
        {
            var result = photoService.CreatePhoto(id, options);

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

            return(Json(result.Data));
        }