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));
        }
        public async Task <IActionResult> Delete([FromRoute] string id)
        {
            string         userId = this.GetUserId();
            LoverCloudUser user   = await _userRepository.FindByIdAsync(userId);

            MenstruationLog mlog = await _mlogRepository.FindByIdAsync(
                id, x => x.Include(l => l.LoverCloudUser)
                .Include(l => l.MenstruationDescriptions));

            if (mlog == null)
            {
                return(NotFound());
            }
            if (mlog?.LoverCloudUser?.Sex == Sex.Male ||
                mlog.LoverCloudUser?.Id != this.GetUserId())
            {
                return(Forbid());
            }

            _mlogRepository.Delete(mlog);

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

            return(NoContent());
        }
        public async Task <IActionResult> PartiallyUpdate(
            [FromRoute] string id,
            [FromBody] JsonPatchDocument <MenstruationLogUpdateResource> patchDoc)
        {
            MenstruationLog mlog = await _mlogRepository.FindByIdAsync(
                id,
                x => x.Include(l => l.LoverCloudUser));

            if (mlog == null)
            {
                return(NotFound());
            }
            if (mlog.LoverCloudUser?.Sex == Sex.Male ||
                mlog.LoverCloudUser?.Id != this.GetUserId())
            {
                return(Forbid());
            }

            MenstruationLogUpdateResource mlogUpdateResource = _mapper.Map <MenstruationLogUpdateResource>(mlog);

            patchDoc.ApplyTo(mlogUpdateResource);
            _mapper.Map(mlogUpdateResource, mlog);

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

            return(NoContent());
        }
        public void Delete(MenstruationLog entity)
        {
            foreach (MenstruationDescription description in entity.MenstruationDescriptions)
            {
                _dbContext.MenstruationDescriptions.Remove(description);
            }

            _dbContext.MenstruationLogs.Remove(entity);
        }
        public async Task <IActionResult> Add(
            [FromRoute] string menstruationLogId,
            [FromBody] MenstruationDescriptionAddResource addResource)
        {
            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            MenstruationLog mlog = await _mlogRepository.FindByIdAsync(
                menstruationLogId,
                c => c.Include(x => x.LoverCloudUser));

            if (mlog == null)
            {
                return(NotFound());
            }
            if (mlog.LoverCloudUser?.Sex == Sex.Male || // 禁止男性用户
                mlog.LoverCloudUser?.Id != this.GetUserId())
            {
                return(Forbid());
            }

            MenstruationDescription mDescription = _mapper.Map <MenstruationDescription>(addResource);

            mDescription.Date            = DateTime.Now;
            mDescription.MenstruationLog = mlog;
            _mdescriptionRepository.Add(mDescription);

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

            MenstruationDescriptionResource mDescriptionResource = _mapper.Map <MenstruationDescriptionResource>(mDescription);

            var result = mDescriptionResource.ToDynamicObject()
                         .AddLinks(
                this, null, "menstruation_description", null,
                "DeleteMenstruationDescription", "PartiallyUpdateMenstruationDescription");

            return(CreatedAtRoute("AddMenstruationDescription", result));
        }
 public void Update(MenstruationLog entity)
 {
     _dbContext.MenstruationLogs.Update(entity);
 }
 public void Add(MenstruationLog entity)
 {
     _dbContext.MenstruationLogs.Add(entity);
 }