Home > Mobile >  I want to limit the user input by date
I want to limit the user input by date

Time:12-22

I'm trying to limit the input of the user by Date, for example if the first input had this date: 2022-12-20, it would only accept dates in a weeks time, so the last possible date would be: 2022-12-27, 7 days later. I think I have to use a query to do this cause I understand that Hibernate does not have a function for such.

So I want to limit data input by date, example: first entry is 2022-12-20 I can only use 2022-12-21/22/23/.../27

this is the entity that I want to limit:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "planDay")
@JsonIgnoreProperties(value = {"planDaysId"})
public class PlanDay {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long planDaysId;

    @Column
    private Date planDate;

    @Column
    private String weekday;

    @ManyToOne
    @JoinColumn(name = "planId", nullable = false)
    private Plans plans;

    @ManyToOne
    @JoinColumn(name = "medsId", nullable = false)
    private Meds meds;

    @Column
    private int medsToTake;
}

where planDate would be the attribute to limit. To save it I just do a planDayRepo.save(). Any Ideas?

Also my Controller and Service: Controller:

@PostMapping(value = "/plan/save")
    public String savePlans(@RequestBody List<PlanDay> plans) {

        return saveService.savePlans(plans);
    }

Service (the for is just so I can update the piilNumber ignore it, I know I should not do it like that):

   public String savePlans(List<PlanDay> plans) {
        for (int i = 0; i < plans.size(); i  ) {

            PlanDay planDay = plans.get(i);
            long medsId = planDay.getMeds().getMedsId();
            int medsToTake = planDay.getMedsToTake();
            int pillNumber = medsRepo.getReferenceById(medsId).getPillNumber();
            int pillUpdate = pillNumber - medsToTake;
            Meds updatePlanDay = medsRepo.findById(medsId).get();
            if (pillUpdate > 0) {
                updatePlanDay.setPillNumber(pillUpdate);
            } else {
                return "Error: No piils available";
            }
            planDayRepo.save(planDay);
        }

Kind Regards

CodePudding user response:

You can use the @PastOrPresent and @FutureOrPresent constraints to ensure that the planDate is either in the past or present, or in the future or present, respectively. You can also use the @Min and @Max constraints to specify the minimum and maximum values allowed for the planDate field.

Here's an example of how you can modify your PlanDay entity class to add the validation constraints:

import javax.validation.constraints.*;

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "planDay")
@JsonIgnoreProperties(value = {"planDaysId"})
public class PlanDay {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long planDaysId;

@Column
@PastOrPresent
@FutureOrPresent(groups = OneWeekInTheFuture.class)
@Min(value = 2022-12-20, groups = OneWeekInTheFuture.class)
@Max(value = 2022-12-27, groups = OneWeekInTheFuture.class)
private Date planDate;

    @Column
    private String weekday;

    @ManyToOne
    @JoinColumn(name = "planId", nullable = false)
    private Plans plans;

    @ManyToOne
    @JoinColumn(name = "medsId", nullable = false)
    private Meds meds;

    @Column
    private int medsToTake;

}

  • Related