public async Task <IActionResult> Add([FromBody] MenstruationLogAddResource addResource)
        {
            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            LoverCloudUser user = await _userRepository.FindByIdAsync(this.GetUserId());

            MenstruationLog mlog = _mapper.Map <MenstruationLog>(addResource);

            mlog.LoverCloudUser = user;
            _mlogRepository.Add(mlog);
            if (!await _unitOfWork.SaveChangesAsync())
            {
                throw new Exception("Failed to add menstruation log.");
            }

            MenstruationLogResource mlogResource = _mapper.Map <MenstruationLogResource>(mlog);
            ExpandoObject           result       = mlogResource.ToDynamicObject()
                                                   .AddLinks(this, null, "menstruation_log", null,
                                                             "DeleteMenstruationLog", "PartiallyUpdateMenstruationLog");

            return(CreatedAtRoute("AddMenstruationLog", result));
        }
示例#2
0
        public async Task <IActionResult> Add(
            [FromBody] LoverAnniversaryAddResource addResource)
        {
            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            LoverCloudUser user = await _userRepository.FindByIdAsync(this.GetUserId());

            if (user.Lover == null)
            {
                return(this.UserNoLoverResult(user));
            }

            LoverAnniversary anniversary = _mapper.Map <LoverAnniversary>(addResource);

            anniversary.Lover = user.Lover;
            _anniversaryRepository.Add(anniversary);

            if (!await _unitOfWork.SaveChangesAsync())
            {
                throw new Exception("Failed to add anniversary");
            }

            ExpandoObject anniversaryResource = _mapper.Map <LoverAnniversaryResource>(anniversary)
                                                .ToDynamicObject();

            (anniversaryResource as IDictionary <string, object>).Add(
                "links", CreateLinksForAnniversary(anniversary.Id));

            return(CreatedAtRoute("AddLoverAnniversary", anniversaryResource));
        }
        public async Task <IActionResult> Get(
            [FromRoute] string menstruationLogId,
            [FromQuery] MenstruationDescriptionParameters parameters)
        {
            LoverCloudUser user = await _userRepository.FindByIdAsync(
                this.GetUserId());

            if (user.Sex == Sex.Male)
            {
                return(Forbid());
            }

            PaginatedList <MenstruationDescription> descriptions =
                await _mdescriptionRepository.GetAsync(
                    user.Id, menstruationLogId, parameters);

            IEnumerable <MenstruationDescriptionResource> descriptionResources =
                _mapper.Map <IEnumerable <MenstruationDescriptionResource> >(descriptions);

            IEnumerable <ExpandoObject> shapedDescriptionResources =
                descriptionResources.ToDynamicObject(parameters.Fields);

            var result = new
            {
                value = shapedDescriptionResources,
                links = this.CreatePaginationLinks(
                    "GetMenstruationDescriptions", parameters,
                    descriptions.HasPrevious, descriptions.HasNext)
            };

            this.AddPaginationHeaderToResponse(descriptions);

            return(Ok(result));
        }
示例#4
0
        public async Task <IActionResult> Add([FromForm] LoverPhotoAddResource loverPhotoAddResource)
        {
            IFormFile file = loverPhotoAddResource.File;

            if (file == null)   // 表单中必须包含图片文件
            {
                ModelState.AddModelError("loverPhotoAddResource", $"parameter {file} cannot be null");
                return(BadRequest(ModelState));
            }

            // 无法自动映射表单的Tags到对应的Tags集合属性, 所以手动处理一下, 读取key为Tags的值, 反序列化json
            // 元数据是json数组, 示例: [{"name": "value"}, {"name", "value2"}]
            // 表单中只能有一个tags键
            Request.Form.TryGetValue("tags", out StringValues tagsStrings);
            if (tagsStrings.Count > 1)
            {
                return(BadRequest());
            }
            if (tagsStrings.Count <= 0)
            {
                tagsStrings = new StringValues("[]");
            }
            IList <TagAddResource> tags =
                JsonConvert.DeserializeObject <IList <TagAddResource> >(tagsStrings.FirstOrDefault());

            loverPhotoAddResource.Tags = tags;

            LoverCloudUser user = await _userRepository.FindByIdAsync(this.GetUserId());

            Lover lover = user.Lover;

            LoverPhoto loverPhoto = _mapper.Map <LoverPhoto>(loverPhotoAddResource);

            // 生成 PhotoPhysicalPath 要用到 Uploader, 所以先设置 Uploader 的值
            loverPhoto.Uploader     = user;
            loverPhoto.Lover        = lover;
            loverPhoto.PhysicalPath = loverPhoto.GeneratePhotoPhysicalPath(file.GetFileSuffix());;
            loverPhoto.UpdateDate   = DateTime.Now;
            loverPhoto.PhotoUrl     = Url.LinkRelative("GetPhoto", new { id = loverPhoto.Id });
            // 添加到数据库
            _repository.Add(loverPhoto);
            if (!await _unitOfWork.SaveChangesAsync())
            {
                throw new Exception("数据保存失败");
            }
            // 保存图片文件
            await file.SaveToFileAsync(loverPhoto.PhysicalPath);

            LoverPhotoResource loverPhotoResource = _mapper.Map <LoverPhotoResource>(loverPhoto);
            ExpandoObject      result             = loverPhotoResource.ToDynamicObject()
                                                    .AddLinks(this, null, "photo", "GetPhoto", "DeleteLoverPhoto", "PartiallyUpdateLoverPhoto");

            return(CreatedAtRoute("AddLoverPhoto", result));
        }
        protected override async Task HandleRequirementAsync(
            AuthorizationHandlerContext context,
            MustFemaleRequirement requirement)
        {
            LoverCloudUser user = await _userRepository.FindByIdAsync(context.User.GetUserId());

            if (user != null && user.Sex == Sex.Female)
            {
                context.Succeed(requirement);
            }
        }
        public async Task <IActionResult> GetProfileImage([FromRoute] string userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                return(BadRequest("user id not assigned"));
            }
            //string loggedUserId = this.GetUserId();
            LoverCloudUser user = await _repository.FindByIdAsync(userId);

            if (user == null)
            {
                return(NotFound($"找不到id为 {userId} 的用户"));
            }

            if (string.IsNullOrEmpty(user.ProfileImagePhysicalPath) ||
                !System.IO.File.Exists(user.ProfileImagePhysicalPath))
            {
                return(NotFound($"用户 {user} 还没有上传头像"));
            }

            return(PhysicalFile(user.ProfileImagePhysicalPath, "image/png"));
        }
示例#7
0
        public async Task <IActionResult> Post(LoverRequestAddResource addResource)
        {
            var requester = await _userRepository.FindByIdAsync(this.GetUserId());

            if (requester == null)
            {
                return(Unauthorized());
            }
            // 被请求方已经接收到过该用户的情侣请求
            if (requester.LoverRequests.Any(x => x.ReceiverId.Equals(addResource.ReceiverId)))
            {
                return(BadRequest());
            }

            var receiver = await _userManager.FindByIdAsync(addResource.ReceiverId);

            if (receiver == null)
            {
                return(BadRequest($"找不到Guid为 {addResource.ReceiverId} 的用户"));
            }

            if (receiver.Lover != null)
            {
                return(BadRequest("对方已有情侣, 无法发出请求."));
            }

            var loverRequest = _mapper.Map <LoverRequest>(addResource);

            loverRequest.Receiver    = receiver;
            loverRequest.Requester   = requester;
            loverRequest.Succeed     = null;
            loverRequest.RequestDate = DateTime.Now;

            _repository.Add(loverRequest);
            var result = await _unitOfWork.SaveChangesAsync();

            if (!result)
            {
                throw new Exception();
            }
            var loverRequestResource = _mapper.Map <LoverRequestResource>(loverRequest);

            return(CreatedAtRoute("AddLoverRequest", loverRequestResource));
        }
        public async Task <IActionResult> Add([FromForm] LoverLogAddResource addResource)
        {
            LoverCloudUser user = await _userRepository.FindByIdAsync(this.GetUserId());

            Lover lover = user.Lover;

            LoverLog loverLog = _mapper.Map <LoverLog>(addResource);

            loverLog.Creater        = user;
            loverLog.Lover          = lover;
            loverLog.CreateDateTime = DateTime.Now;
            loverLog.LastUpdateTime = DateTime.Now;
            _repository.Add(loverLog);
            if (addResource.Photos != null)
            {
                foreach (var formFile in addResource.Photos)
                {
                    var photo = new LoverPhoto
                    {
                        Name           = formFile.FileName,
                        Uploader       = user,
                        Lover          = lover,
                        LoverLog       = loverLog,
                        PhotoTakenDate = DateTime.Now,
                    };
                    photo.PhysicalPath = photo.GeneratePhotoPhysicalPath(formFile.GetFileSuffix());
                    loverLog.LoverPhotos.Add(photo);
                    await formFile.SaveToFileAsync(photo.PhysicalPath);
                }
            }

            if (!await _unitOfWork.SaveChangesAsync())
            {
                return(NoContent());
            }

            LoverLogResource loverLogResource       = _mapper.Map <LoverLogResource>(loverLog);
            ExpandoObject    shapedLoverLogResource = loverLogResource.ToDynamicObject()
                                                      .AddLinks(
                this, null, "log", "GetLoverLog",
                "DeleteLoverLog", "PartiallyUpdateLoverLog");

            return(CreatedAtRoute("AddLoverLog", shapedLoverLogResource));
        }
示例#9
0
 private Task <LoverCloudUser> GetUserAsync()
 {
     return(_userRepository.FindByIdAsync(this.GetUserId()));
 }